【问题标题】:Configure persistence.xml JPA配置persistence.xml JPA
【发布时间】:2017-08-07 13:29:29
【问题描述】:

我想为 OneToMany 关系配置 persistence.xml 文件。我的 IDE 是 NetBeans。我的坚持是这样的:

 <?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">
  <!-- Will be referenced in Spring Context File -->
  <persistence-unit name="jpa-persistence" transaction-type="RESOURCE_LOCAL">
    <class>co.trainingproject.models.User</class>
    <class>co.trainingproject.models.Category</class>
    <class>co.trainingproject.models.Topic</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/trainingdb"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.password" value="123456"/>
    </properties>
  </persistence-unit>
</persistence>

还有 Category.java:

@OneToMany(mappedBy = "idcategory", cascade = CascadeType.ALL)
private List<Topic> topiclist;

还有Topic.java:

@ManyToOne
@JoinColumn(name = "idcategory", referencedColumnName = "idcategory")
private Category idcategory;

ApplicationContext.xml:

<?xml version="1.0" encoding="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:context="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">
    <!-- Enable Spring Annotation Configuration -->
    <context:annotation-config />
    <!-- Scan for all of Spring components such as Spring Service -->
    <context:component-scan base-package="co.trainingproject"></context:component-scan>

    <!-- Necessary to get the entity manager injected into the factory bean -->
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 

    <!-- Define EclipseLink JPA Vendor Adapter -->
    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
        <property name="databasePlatform"
            value="org.eclipse.persistence.platform.database.MySQLPlatform" />
        <property name="generateDdl" value="false" />
        <property name="showSql" value="true" />
    </bean>

    <!-- Define Hibernate JPA Vendor Adapter -->
    <!-- <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform"
            value="org.hibernate.dialect.MySQLDialect" />
    </bean>  -->

    <!-- Entity Manager Factory -->
    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="jpa-persistence"></property>
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
    </bean>

    <!-- Transaction Manager -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>

    <!-- Detect @Transactional -->
    <tx:annotation-driven transaction-manager="transactionManager" />
</beans>

我认为这些文件中需要另外设置,因为当我想在 glassfish 中部署我的项目时,出现了这个错误:

Exception Occurred :Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryService': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)

【问题讨论】:

  • 您应该检查的几件事 1. CategoryService 是否在包“co.trainingproject”下。 2. CategoryService 是否使用@Component 或等效注解。
  • 是的,我有一个名为:co.trainingproject.services 的包,其中包含一些服务类。 @eatSleepCode

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 1970-01-01
    • 2012-01-06
    • 2023-04-08
    • 1970-01-01
    • 2012-05-01
    • 2010-11-11
    • 2012-01-16
    • 2011-02-09
    相关资源
    最近更新 更多