programing

Hibernate 문제- "매핑되지 않은 클래스를 대상으로하는 @OneToMany 또는 @ManyToMany 사용"

nasanasas 2020. 8. 21. 07:51
반응형

Hibernate 문제- "매핑되지 않은 클래스를 대상으로하는 @OneToMany 또는 @ManyToMany 사용"


Hibernate Annotations로 내 발을 찾고 있는데 누군가가 도움을 줄 수 있기를 바라는 문제에 부딪 혔습니다.

2 개의 엔티티, Section 및 ScopeTopic이 있습니다. 섹션에는 List 클래스 멤버가 있으므로 일대 다 관계입니다. 단위 테스트를 실행할 때이 예외가 발생합니다.

매핑되지 않은 클래스를 대상으로하는 @OneToMany 또는 @ManyToMany 사용 : com.xxx.domain.Section.scopeTopic [com.xxx.domain.ScopeTopic]

오류가 내 ScopeTopic 엔터티가 테이블에 매핑되지 않았 음을 의미한다고 가정합니다. 나는 내가 잘못한 것을 볼 수 없습니다. 다음은 Entity 클래스입니다.


@Entity
public class Section {
    private Long id;
    private List<ScopeTopic> scopeTopics;

    public Section() {}

    @Id
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @OneToMany
    @JoinTable(name = "section_scope", joinColumns = {@JoinColumn(name="section_id")},
               inverseJoinColumns = {@JoinColumn(name="scope_topic_id")} )
    public List<ScopeTopic> getScopeTopic() {
        return scopeTopic;
    }

    public void setScopeTopic(List<ScopeTopic> scopeTopic) {
        this.scopeTopic = scopeTopic;
    }
}

@Entity
@Table(name = "scope_topic")
public class ScopeTopic {
    private Long id;
    private String topic;

    public ScopeTopic() {}

    @Id
    public Long getId() {
        return id;
    }

    public void setId() {
        this.id = id;
    }

    public String getTopic() {
        return topic;
    }

    public void setTopic(String topic) {
        this.topic = topic;
    }
}

나는 내 자신의 이해 부족이 잘못이라고 확신하므로 약간의 지침이 좋을 것입니다. 감사합니다!


주석이 괜찮아 보입니다. 확인해야 할 사항은 다음과 같습니다.

  • make sure the annotation is javax.persistence.Entity, and not org.hibernate.annotations.Entity. The former makes the entity detectable. The latter is just an addition.

  • if you are manually listing your entities (in persistence.xml, in hibernate.cfg.xml, or when configuring your session factory), then make sure you have also listed the ScopeTopic entity

  • make sure you don't have multiple ScopeTopic classes in different packages, and you've imported the wrong one.


Mine was not having @Entity on the many side entity

@Entity // this was commented
@Table(name = "some_table")
public class ChildEntity {
    @JoinColumn(name = "parent", referencedColumnName = "id")
    @ManyToOne
    private ParentEntity parentEntity;
}

Your entity may not listed in hibernate configuration file.


Mostly in Hibernate , need to add the Entity class in hibernate.cfg.xml like-

<hibernate-configuration>
  <session-factory>

    ....
    <mapping class="xxx.xxx.yourEntityName"/>
 </session-factory>
</hibernate-configuration>

I had the same problem and I could solve it by adding the entity into persistence.xml. The problem was caused due to the fact that the entity was not added to the persistence config. Edit your persistence file:

<persistence-unit name="MY_PU" transaction-type="RESOURCE_LOCAL">
<provider>`enter code here`
     org.hibernate.jpa.HibernatePersistenceProvider
  </provider>
<class>mypackage.MyEntity</class>

...


In my case a has to add my classes, when building the SessionFactory, with addAnnotationClass

Configuration configuration.configure();
StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
SessionFactory sessionFactory = configuration
            .addAnnotatedClass(MyEntity1.class)
            .addAnnotatedClass(MyEntity2.class)
            .buildSessionFactory(builder.build());

If you are using Java configuration in a spring-data-jpa project, make sure you are scanning the package that the entity is in. For example, if the entity lived com.foo.myservice.things then the following configuration annotation below would not pick it up.

You could fix it by loosening it up to just com.foo.myservice (of course, keep in mind any other effects of broadening your scope to scan for entities).

@Configuration
@EnableJpaAuditing
@EnableJpaRepositories("com.foo.myservice.repositories")
public class RepositoryConfiguration {
}

참고URL : https://stackoverflow.com/questions/4956855/hibernate-problem-use-of-onetomany-or-manytomany-targeting-an-unmapped-clas

반응형