配置persistence.xml JPA

时间:2017-08-07 13:29:30

标签: xml jpa netbeans eclipselink one-to-many

我想为OneToMany关系配置persistence.xml文件。我的IDE是NetBeans。我的坚持是这样的:  <?xml version =" 1.0"编码=" 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">   <! - 将在Spring Context File中引用 - >   < persistence-unit name =" jpa-persistence"交易类型=" RESOURCE_LOCAL">     <类> co.trainingproject.models.User< /类>     <类> co.trainingproject.models.Category< /类>     <类> co.trainingproject.models.Topic< /类>     <性状>       < property name =" javax.persistence.jdbc.url"值=" JDBC:MySQL的://本地主机:3306 / trainingdb" />       < property name =" javax.persistence.jdbc.user"值="根" />       < property name =" javax.persistence.jdbc.driver"值=" com.mysql.jdbc.Driver" />       < property name =" javax.persistence.jdbc.password"值=#&34; 123456" />     < /性状>   < /持久性单元> < /持久> 和Category.java: @OneToMany(mappedBy =" idcategory",cascade = CascadeType.ALL) 私人清单<主题>题目列表; 和Topic.java: @ManyToOne @JoinColumn(name =" idcategory",referencedColumnName =" idcategory") 私人类别idcategory; applicationContext.xml中: <?xml version =" 1.0"编码=" UTF-8"> < beans xmlns =" http://www.springframework.org/schema/beans"     的xmlns:=的xsi" HTTP://www.w3.org/2001/XMLSchema-instance"的xmlns:TX =" HTTP://www.springframework.org/schema/tx"     的xmlns:上下文=" HTTP://www.springframework.org/schema/context"     的xmlns:AOP =" HTTP://www.springframework.org/schema/aop"的xmlns:UTIL =" HTTP://www.springframework.org/schema/util"     xsi:schemaLocation =" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/ schema / aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring -context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">     <! - 启用Spring注释配置 - >     < context:annotation-config />     <! - 扫描所有Spring组件,例如Spring Service - >     < context:component-scan base-package =" co.trainingproject">< / context:component-scan>     <! - 将实体管理器注入工厂bean所必需的 - >     < bean class =" org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />     <! - 定义EclipseLink JPA供应商适配器 - >     < bean id =" jpaVendorAdapter"         类=" org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">         < property name =" databasePlatform"             值=" org.eclipse.persistence.platform.database.MySQLPlatform" />         < property name =" generateDdl"值="假" />         < property name =" showSql"值="真" />     < /豆腐>     <! - 定义Hibernate JPA供应商适配器 - >     <! - < bean id =" jpaVendorAdapter"         类=" org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">         < property name =" databasePlatform"             值=" org.hibernate.dialect.MySQLDialect" />     < /豆腐> - >     <! - 实体经理工厂 - >     < bean id =" entityManagerFactory"         类=" org.springframework.orm.jpa.LocalEntityManagerFactoryBean">         < property name =" persistenceUnitName"值=" JP-A-持久">< /性>         < property name =" jpaVendorAdapter" REF =" jpaVendorAdapter" />     < /豆腐>     <! - 事务管理器 - >     < bean id =" transactionManager"类=" org.springframework.orm.jpa.JpaTransactionManager">         < property name =" entityManagerFactory" REF ="的entityManagerFactory" />     < /豆腐>     <! - 检测@Transactional - >     < tx:annotation-driven transaction-manager =" transactionManager" /> < /豆类> 我认为这些文件需要另一个设置,因为当我想在glassfish中部署我的项目时,会发生以下错误: 发生异常:部署期间发生错误:加载应用程序时出现异常:java.lang.IllegalStateException:ContainerBase.addChild:start:org.apache.catalina.LifecycleException:org.springframework.beans.factory.BeanCreationException:创建名称为&的bean时出错#39; categoryService&#39 ;:持久性依赖注入失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为' entityManagerFactory'的bean时出错。在ServletContext资源[/WEB-INF/applicationContext.xml]中定义:init方法的调用失败;嵌套异常是Exception [EclipseLink-30005](Eclipse Persistence Services - 2.5.0.v20130507-3faac2b):org.eclipse.persistence.exceptions.PersistenceUnitLoadingException 异常描述:使用ClassLoader搜索持久性归档时抛出异常:WebappClassLoader(delegate = true; repositories = WEB-INF / classes /)

1 个答案:

答案 0 :(得分:0)

此错误与NetBeans有关。看this. 我在glassfish4 / bin中写道:

./asadmin add-resources ~/NetBeansProjects/../web/WEB-INF/glassfish-resources.xml