EclipseLink JPA未知的实体bean类

时间:2015-07-20 13:15:47

标签: java spring jpa eclipselink

我有一个实体类作者

@Entity
@Table(name = "author")
public class Author implements Serializable {

private static final long serialVersionUID = -1117370177012369440L;

  @Id
  @GeneratedValue
  @Column(name = "author_id")
  private Long id;

  @NotNull
  @Size(max = 30)
  @Column(name = "author_name")
  private String name; 

  @Column(name = "expired")
  private Date expired;

  @OneToMany
@JoinTable(
        name="news_author",
        joinColumns={ @JoinColumn(name="author_id", referencedColumnName="author_id") },
        inverseJoinColumns={ @JoinColumn(name="news_id", referencedColumnName="news_id", unique=true) }
)
private Set<News> news;
}

新闻:

@Entity
@Table(name = "news")
public class News implements Serializable {

private static final long serialVersionUID = -2401617129682260390L;

@Id
@GeneratedValue
@Column(name = "news_id")
private Long id;
@NotNull
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "creation_date")
private Date creationDate;

@ManyToOne
private Author author;
}

这是我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="newsmanagement" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.newsmanagement.domain.Author</class>
<properties>
    <property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
    <property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
    <property name="javax.persistence.jdbc.user" value="TEST"/>
    <property name="javax.persistence.jdbc.password" value="root"/>
    <property name="eclipselink.weaving" value="false"/>
</properties>

这是我的弹簧上下文配置:

<?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:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<context:component-scan base-package="com.newsmanagement" />

<!-- Add JPA support -->
<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"/>
    </property>
    <property name="persistenceUnitName" value="newsmanagement" />
</bean>

<!-- Add Transaction support -->
<bean id="tx-manager"
      class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf"/>
</bean>

<!-- Use @Transaction annotations for managing transactions -->
<tx:annotation-driven transaction-manager="tx-manager" />

<bean id="jpaDialect"
      class="org.springframework.orm.jpa.vendor.EclipseLinkJpaDialect " />

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor "/>

</beans>

然后在DAO课程中,我想通过id找到作者:

@Repository
public class AuthorDAOImpl implements AuthorDAO {

@PersistenceContext
private EntityManager entityManager;

@Override
public Author findById(Long id) {
    return entityManager.find(Author.class, id);
}
}

而不是我得到以下异常:

java.lang.IllegalArgumentException: Unknown entity bean class: class com.newsmanagement.domain.Author, please verify that this class has been marked with the @Entity annotation.

Author类上的注释一切正常。注释来自org.eclipse.persistence,我已经通过maven包含了这个注释。 问题在哪里?

0 个答案:

没有答案