hibernate中SchemaExport方式创建表
[test]
package test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.Test;
public class testUsers {
@Test
public void testSchemaExport() {
SessionFactory sessionFactory = new AnnotationConfiguration()
.configure().buildSessionFactory();
SchemaExport export = new SchemaExport(
new AnnotationConfiguration().configure());
export.create(true, false);
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.getTransaction().commit();
}
}
[hibernate.cfg.xml] 这里注意去掉hbm2ddl.auto
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost/mydb
root
root
1
org.hibernate.dialect.MySQLDialect
thread
org.hibernate.cache.NoCacheProvider
true
[Users]
package pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
@Entity
public class Users {
private int id;
private String level;
private String title;
private Customer customer;
@OneToOne
@JoinColumn(name="customer____tables")
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Id
@GeneratedValue
public int getId() {
return id;
}
public String getLevel() {
return level;
}
public String getTitle() {
return title;
}
public void setId(int id) {
this.id = id;
}
public void setLevel(String level) {
this.level = level;
}
public void setTitle(String title) {
this.title = title;
}
}
控制台:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
drop table if exists Customer
create table Customer (id integer not null auto_increment, name varchar(255), password varchar(255), primary key (id))
mysql:
浙公网安备 33010602011771号