복붙노트

[SPRING] 봄 + 최대 절전 모드 = 알 수없는 엔티티

SPRING

봄 + 최대 절전 모드 = 알 수없는 엔티티

주석을 사용하여 Spring과 Hibernate를 결합하려고하는데 다음과 같은 오류가 발생합니다.

org.springframework.orm.hibernate3.HibernateSystemException : Unknown entity: entities.Bar; nested exception is org.hibernate.MappingException: Unknown entity: entities.Bar

여기에 내 설치가있다 ...

내 실체 :

package entities;

@Entity    
public class Bar implements Serializable
{
  ...
}

내 콩 :

package blah;

@Repository
@Service("fooService")
@RemotingDestination(channels = { "my-amf" })
public class Foo
{
  protected HibernateTemplate template;

  @Autowired
  public void setSessionFactory(SessionFactory sessionFactory)
  {
    template = new HibernateTemplate(sessionFactory);
  }

  @RemotingInclude
  public void addBar(String name) throws DataAccessException
  {
    Bar bar = new Bar();
    bar.setName(name);
    template.save(bar);
  }

}

스프링에서 특수 효과를 사용할 수 있습니다.

<context:annotation-config />
<context:component-scan base-package="blah" />

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="org.h2.Driver" />
    <property name="url" value="jdbc:h2:~/blahdb/blahdb" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>entities.Bar</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>
</bean>

BlazeDS를 통해 Flex 어플리케이션에서 내 Foo.addBar 메소드를 호출하면 오류가 발생합니다.

정말 추가 구성을 피하고 싶습니다 그리고 모든이 작동해야합니다 것 같습니다.

저는 Spring 3.0.0.RC1, Hibernate Annotations 3.4.0, Tomcat 6.0.20, Java 1.6.0_15를 사용하고 있습니다.

어떤 아이디어? 감사.

해결법

  1. ==============================

    1.엔티티 주석에 org.hibernate.annotations.Entity가 아닌 import @ javax.persistence.Entity를 사용해보십시오.

    엔티티 주석에 org.hibernate.annotations.Entity가 아닌 import @ javax.persistence.Entity를 사용해보십시오.

  2. ==============================

    2.나는 같은 문제를 겪었으며 이것에 대한 좋은 대답을 찾지 못했다.

    나는 같은 문제를 겪었으며 이것에 대한 좋은 대답을 찾지 못했다.

    나를 위해 일한 것은 persistence.xml 파일에 내 엔티티 클래스를 선언하는 것이 었습니다.

    (자원 및 테스트 모두) :

    <persistence ...>
        <persistence-unit ...>
    
            <class>com.company.maenad.core.model.News</class>
            <class>com.company.maenad.core.model.AdExtraInfo</class>
    
        </persistence-unit>
    </persistence>
    
  3. ==============================

    3.sessionFactory를 구성하는 데 사용 된 annotatedClasses 속성이 패키지의 오른쪽 엔터티를 가리키고 있지 않은 경우 위에서 언급 한 Exception도 발생합니다.

    sessionFactory를 구성하는 데 사용 된 annotatedClasses 속성이 패키지의 오른쪽 엔터티를 가리키고 있지 않은 경우 위에서 언급 한 Exception도 발생합니다.

    annotatedClasses 대신에 packagesToScan 속성을 사용하는 것이 좋습니다. Entities에 대한 전체 패키지를 검사하므로 정규화 된 클래스 이름이있는 Entity에 대한 명시적인 언급을 피할 수 있습니다.

    <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="packagesToScan" value="com.code.entity"></property>
    <property name="hibernateProperties">
    <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        <prop key="hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>
    

  4. ==============================

    4.내가 명시 적으로 entites의 패키지를 작성하는 것이 안전하다고 생각합니다. 이 오류가 발생하여 EntityScan 주석을 사용하여 해결했습니다.

    내가 명시 적으로 entites의 패키지를 작성하는 것이 안전하다고 생각합니다. 이 오류가 발생하여 EntityScan 주석을 사용하여 해결했습니다.

    @EntityScan(basePackages = "your.entities.pakage")
    
  5. ==============================

    5.Spring 애플리케이션 컨텍스트 XML에 적절한 네임 스페이스를 추가했는지 확인하십시오.

    Spring 애플리케이션 컨텍스트 XML에 적절한 네임 스페이스를 추가했는지 확인하십시오.

        <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd>
    
  6. ==============================

    6.내가 생각할 수있는 유일한 것은 annotatedClasses 정의에 문제의 엔터티가 누락 된 것입니다. 패키지 이름을 포함하여 annotatedClasses def를 다시 확인할 수 있습니까?

    내가 생각할 수있는 유일한 것은 annotatedClasses 정의에 문제의 엔터티가 누락 된 것입니다. 패키지 이름을 포함하여 annotatedClasses def를 다시 확인할 수 있습니까?

    이 오류가 시작될 때가오고 있다고 생각하는 것이 맞습니까? 오류 메시지 주위에 좀 더 많은 컨텍스트를 포함시킬 수 있습니까? 예를 들어, annotatedClasses 정의에서 클래스 중 하나를 제거하여보고 대상과 비슷한 것을 재현 할 수있었습니다.

    2009-11-01 10:05:55.593::WARN:  Nested in org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invo
    cation of init method failed; nested exception is org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.springinpractice.ch06.model.Message.forum references an unknown entity: com.springinpractice.ch06.model.
    Forum:
    org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.springinpractice.ch06.model.Message.forum references an unknown entity: com.springinpractice.ch06.model.Forum
            at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:81)
            at org.hibernate.cfg.AnnotationConfiguration.processEndOfQueue(AnnotationConfiguration.java:456)
    

    [한조각]

    편집 : 다른 질문 / 아이디어. 런타임 클래스 패스에 적절한 주석 JAR (JPA의 경우 persistence.jar 또는 JAR의 Hibernate) 중 하나가 있습니까?

    다른 편집 : 하나 더. 어떤 JVM 버전을 실행하고 있습니까?

  7. from https://stackoverflow.com/questions/1657557/spring-hibernate-unknown-entity by cc-by-sa and MIT license