我有一个简单的控制台应用程序与JPA。
package com.concretepage.entity;
import javax.persistence.*;
@Entity
@Table(name="farmer")
@NamedQuery(name = "farmer.getAll", query = "SELECT a from Farmer a")
public class Farmer {
@Id
@Column(name="id")
private int id;
@Column(name="name")
private String name;
@Column(name="village")
private String village;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVillage() {
return village;
}
public void setVillage(String village) {
this.village = village;
}
}
package com.concretepage;
import com.concretepage.entity.Farmer;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import java.util.List;
public class JPADemo {
private static final EntityManagerFactory emFactory;
static {
emFactory = Persistence.createEntityManagerFactory("com.concretepage");
}
public static EntityManager getEntityManager(){
return emFactory.createEntityManager();
}
public static void main(String[] args) {
EntityManager em = getEntityManager();
em.getTransaction().begin();
Farmer farmer = em.find(Farmer.class, 3);
System.out.println(farmer);
TypedQuery<Farmer> namedQuery = em.createNamedQuery("farmer.getAll", Farmer.class);
List<Farmer> farmers = namedQuery.getResultList();
System.out.println(farmers);
System.out.println("Done");
}
}
<persistence 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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="com.concretepage">
<description>JPA Test</description>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.concretepage.entity.Farmer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
</properties>
</persistence-unit>
</persistence>
方法em.find(...)和namedQuery.getResultList()返回true响应! Everythink'is OK :) 但是当我在Web服务应用程序中使用此代码时:
@Stateless
public class TestService {
@PersistenceContext(unitName = "MainDB")
private EntityManager em;
public Farmer add(Farmer farmer){
farmer = em.merge(farmer);
return farmer;
}
public Farmer get(Integer id){
return em.find(Farmer.class, id);
}
public List<Farmer> getAll(){
TypedQuery<Farmer> namedQuery = em.createNamedQuery("Farmer.getAll", Farmer.class);
return namedQuery.getResultList();
}
}
<persistence-unit name="MainDB" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>model.Farmer</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/test"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
</properties>
</persistence-unit>
如果我使用:
<property name="hibernate.hbm2ddl.auto" value="validate"/>
or not use
收到Exeption:org.hibernate.HibernateException:缺少表 如果我使用:
<property name="hibernate.hbm2ddl.auto" value="update"/>
收到空响应em.find(...)和namedQuery.getResultList(),但表和数据都存在! 我如何使用curent数据库和数据?