我有一个简单的maven项目,使用jpa和ejb,它运行在服务器glassfish 4.1
上我的persistence.xml文件是:
last pid: 22963; load averages: 0.56, 0.20, 0.12 up 1+02:30:06
11:39:54
139 processes: 1 running, 127 sleeping, 10 stopped, 1 zombie
CPU: % user, % nice, % system, % interrupt, % idle
Mem: 384M Active, 331M Inact, 198M Wired, 2716K Cache, 87M Buf, 4995M Free
Swap: 4096M Total, 4096M Free
PID USERNAME THR PRI NICE SIZE RES STATE C TIME WCPU COMMAND
789 root 45 20 0 1051M 343M wait 3 2:48 0.00% java
22894 _relayd 3 52 0 132M 67504K nanslp 0 1:03 0.00% relayd
22893 _relayd 3 52 0 128M 64464K nanslp 3 1:02 0.00% relayd
22892 _relayd 3 52 0 132M 68212K nanslp 3 1:01 0.00% relayd
22891 _relayd 3 52 0 132M 70368K nanslp 3 0:58 0.00% relayd
674 root 1 20 0 12080K 1688K select 0 0:42 0.00% syslogd
726 root 2 20 0 28904K 4844K select 0 0:23 0.00% monit
892 pgsql 1 20 0 186M 28876K sbwait 1 0:23 0.00% postgres
890 pgsql 1 20 0 182M 24872K sbwait 3 0:23 0.00% postgres
891 pgsql 1 20 0 182M 24132K sbwait 2 0:20 0.00% postgres
762 root 1 20 0 12088K 1596K select 0 0:17 0.00% powerd
22888 root 1 52 0 90356K 30196K kqread 0 0:06 0.00% relayd
759 root 1 20 0 22264K 3764K select 2 0:05 0.00% ntpd
779 pgsql 1 52 0 178M 17360K select 2 0:04 0.00% postgres
816 root 1 20 0 20344K 4880K select 0 0:04 0.00% sendmail
785 pgsql 1 20 0 37700K 7072K select 2 0:03 0.00% postgres
784 pgsql 1 20 0 178M 18352K select 0 0:02 0.00% postgres

我的实体文件Employee.java:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="EmployeeService" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.bkstorm.jpa.model.Employee</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/jpa" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="123456a@" />
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<!-- Configuring Connection Pool -->
<property name="hibernate.c3p0.min_size" value="5"/>
<property name="hibernate.c3p0.max_size" value="20"/>
<property name="hibernate.c3p0.timeout" value="500"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.idle_test_period" value="2000"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
&#13;
我有一个ejb会话bean: EmployeeService.java
@Entity
@Table(name = "employee")
public class Employee implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "emp_id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "sal")
private int salary;
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 int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
}
}
&#13;
和实现EmployeeService的EmployeeServiceBean.java
public interface EmployeeService {
public Employee createEmployee(String name, int salary);
public Collection<Employee> findAllEmployees();
}
&#13;
的pom.xml
@Stateless
public class EmployeeServiceBean implements EmployeeService {
@PersistenceContext(unitName = "EmployeeService")
protected EntityManager em;
@Override
public Employee createEmployee(String name, int salary) {
Employee emp = new Employee();
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
return emp;
}
@Override
public Collection<Employee> findAllEmployees() {
Query query = em.createQuery("SELECT e FROM Employee e");
return (Collection<Employee>) query.getResultList();
}
}
&#13;
这是我的错误记录:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bkstorm.jpa</groupId>
<artifactId>autoIdGeneration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>autoIdGeneration</name>
<dependencies>
<!-- JPA -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.11.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.3.11.Final</version>
</dependency>
<!-- For connection pooling -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>4.3.11.Final</version>
</dependency>
<!-- Database -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
&#13;
我不知道错误的来源。任何想法,谢谢!
更新1: 改变&#34;验证&#34;到&#34;更新&#34;没有解决我的问题。 employee表存在于我的数据库中。 当我运行glassfish服务器时,它会显示log:
Info: HV000001: Hibernate Validator 5.0.0.Final
Info: HHH000204: Processing PersistenceUnitInfo [
name: EmployeeService
...]
Info: HHH000412: Hibernate Core {5.0.0.Final}
Info: HHH000206: hibernate.properties not found
Info: HHH000021: Bytecode provider name : javassist
Info: HCANN000001: Hibernate Commons Annotations {5.0.0.Final}
Info: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
Info: HHH000229: Running schema validator
Info: HHH000262: Table not found: employee
Info: HHH000262: Table not found: employee
Severe: Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method
Severe: javax.persistence.PersistenceException: [PersistenceUnit: EmployeeService] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:877)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:805)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:199)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.<init>(PersistenceUnitLoader.java:107)
at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:925)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:434)
at com.sun.enterprise.v3.server.ApplicationLoaderService.processApplication(ApplicationLoaderService.java:406)
at com.sun.enterprise.v3.server.ApplicationLoaderService.postConstruct(ApplicationLoaderService.java:243)
at org.jvnet.hk2.internal.ClazzCreator.postConstructMe(ClazzCreator.java:329)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:377)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:461)
at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:227)
at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:84)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2258)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:105)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.oneJob(CurrentTaskFuture.java:1162)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.run(CurrentTaskFuture.java:1147)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$UpOneLevel.run(CurrentTaskFuture.java:753)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [employee]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:67)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:484)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
... 25 more
Severe: Exception while preparing the app
Severe: Exception during lifecycle processing
javax.persistence.PersistenceException: [PersistenceUnit: EmployeeService] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:877)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:805)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:135)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.loadPU(PersistenceUnitLoader.java:199)
at org.glassfish.persistence.jpa.PersistenceUnitLoader.<init>(PersistenceUnitLoader.java:107)
at org.glassfish.persistence.jpa.JPADeployer$1.visitPUD(JPADeployer.java:223)
at org.glassfish.persistence.jpa.JPADeployer$PersistenceUnitDescriptorIterator.iteratePUDs(JPADeployer.java:510)
at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:230)
at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:168)
at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:925)
at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:434)
at com.sun.enterprise.v3.server.ApplicationLoaderService.processApplication(ApplicationLoaderService.java:406)
at com.sun.enterprise.v3.server.ApplicationLoaderService.postConstruct(ApplicationLoaderService.java:243)
at org.jvnet.hk2.internal.ClazzCreator.postConstructMe(ClazzCreator.java:329)
at org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:377)
at org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:461)
at org.glassfish.hk2.runlevel.internal.AsyncRunLevelContext.findOrCreate(AsyncRunLevelContext.java:227)
at org.glassfish.hk2.runlevel.RunLevelContext.findOrCreate(RunLevelContext.java:84)
at org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2258)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:105)
at org.jvnet.hk2.internal.ServiceHandleImpl.getService(ServiceHandleImpl.java:87)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.oneJob(CurrentTaskFuture.java:1162)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$QueueRunner.run(CurrentTaskFuture.java:1147)
at org.glassfish.hk2.runlevel.internal.CurrentTaskFuture$UpOneLevel.run(CurrentTaskFuture.java:753)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing table [employee]
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.validateTable(SchemaValidatorImpl.java:67)
at org.hibernate.tool.schema.internal.SchemaValidatorImpl.doValidation(SchemaValidatorImpl.java:50)
at org.hibernate.tool.hbm2ddl.SchemaValidator.validate(SchemaValidator.java:91)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:484)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
... 25 more
&#13;
然后,我执行EmployeeServiceBean.fillAllEmployees,发生错误。 我不知道发生了什么!
答案 0 :(得分:7)
从&#39;验证&#39;更改hibernate.hbm2ddl.auto值更新&#39;并且hibernate为你创建表。
<property name="hibernate.hbm2ddl.auto" value="update"/>
答案 1 :(得分:2)
试试这个,它对我有用:
<property name="hibernate.hbm2ddl.auto" value="create"/>
答案 2 :(得分:0)
对我而言,定义两个属性看起来很可疑:
type Tree' a = Maybe (Tree'' a)
data Tree'' t = Node' t (Tree' t) (Tree' t)
deriving (Functor)
instance Comonad Tree'' where
extract (Node' x _ _) = x
duplicate t@(Node' _ b1 b2) = Node' t (fmap duplicate b1) (fmap duplicate b2)
和
<property name="hibernate.hbm2ddl.auto" value="validate"/>
这两个属性用于告诉JPA提供程序如何处理架构。 后者从JPA 2.1开始存在,现在是首选的可移植方式,因此我建议删除hibernate特定的属性。