Java/JPA with Error
[해결 방법] org.hibernate.AnnotationException
HJ0216
2023. 8. 16. 12:41
👉 기본 환경
- Language: Java
- DB: H2 Database
- IDE: IntelliJ
⌨️ 코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
import javax.persistence.*;
import java.util.List;
public class Tmp {
@Id @GeneratedValue
@Column(name = "TMP_ID")
private Long id;
@OneToMany(mappedBy = "tmp")
private List<TmpItem> tmpItems;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<TmpItem> getTmpItems() {
return tmpItems;
}
public void setTmpItems(List<TmpItem> tmpItems) {
this.tmpItems = tmpItems;
}
}
|
🖨️오류
1
2
3
4
5
6
7
8
9
10
11
12
13
|
Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on jpabook.jpashop.domain.TmpItem.tmp references an unknown entity: jpabook.jpashop.domain.Tmp
at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:97)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1823)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1767)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1214)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1245)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:56)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at jpabook.jpashop.JpaMain.main(JpaMain.java:15)
|
📡 원인
Entity 클래스 선언 시, @Entity 선언을 하지 않아 인식되지 않음
그에 따라 Entity간 매핑해놓은 @ManyToOne에서 unknown entity exception 발생
📰 해결 방법
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
import javax.persistence.*;
import java.util.List;
@Entity
public class Tmp {
@Id @GeneratedValue
@Column(name = "TMP_ID")
private Long id;
@OneToMany(mappedBy = "tmp")
private List<TmpItem> tmpItems;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<TmpItem> getTmpItems() {
return tmpItems;
}
public void setTmpItems(List<TmpItem> tmpItems) {
this.tmpItems = tmpItems;
}
}
|
Entity 클래스에 @Entity 추가