<property name="hibernate.show_sql">true</property>
@Entity
@Table(name="E_USER", uniqueConstraints={@UniqueConstraint(columnNames={"yahoo"})})
public class User {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String yahoo;
// getter/setter
}
DROP TABLE IF EXISTS `e_user`;
CREATE TABLE `e_user` (
`id` int(11) NOT NULL auto_increment,
`yahoo` varchar(255) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `yahoo` (`yahoo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
<property name="hbm2ddl.auto">create</property>
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
參數:script(打印 DDL)、export(執行建表)。
drop table if exists E_USER
create table E_USER (
id integer not null auto_increment,
yahoo varchar(255),
primary key (id),
unique (yahoo)
)
AnnotationConfiguration 同樣支持 .hbm.xml 混合映射文件。