81,095
社区成员
发帖
与我相关
我的任务
分享
public class NewsDao {
private static final EntityManagerFactory emf =
Persistence.createEntityManagerFactory("qs");
public static void main(String[] args) {
final EntityManager em = emf.createEntityManager();
News news = new News();
news.setTitle("测试title");
em.persist(news);
}
}
@Entity
@Table(name="news_table")
public class News {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="news_title",length=50)
private String title;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd">
<persistence-unit name="qs" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.wjf.entities.News</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<!--
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/testdatabase?useUnicode=true&characterEncoding=UTF-8" />
-->
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/world" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="123456" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create"/>
</properties>
</persistence-unit>
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd">
<persistence-unit name="qs" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.wjf.entities.News</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<!--
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/testdatabase?useUnicode=true&characterEncoding=UTF-8" />
-->
<property name="hibernate.connection.url" value="jdbc.mysql://localhost:3306/world" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="123456" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
测试运行通过,但是数据库中没有建表?
public class NewsTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void createTable(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("qs");
emf.close();
}
}