Java/JPA with Error

[해결 방법] org.hibernate.AnnotationException

HJ0216 2023. 8. 16. 12:57

👉 기본 환경

- 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
30
import javax.persistence.*;
import java.util.List;
 
@Entity
public class Tmp {
 
    @Id @GeneratedValue
    @Column(name = "TMP_ID")
    private Long id;
 
    @OneToMany(mappedBy = "temp")
    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
14
Exception in thread "main" org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: jpabook.jpashop.domain.TmpItem.temp in jpabook.jpashop.domain.Tmp.tmpItems
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:844)
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:795)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:53)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1693)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1661)
    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)
 
 
 

 

 

📡 원인

@OneToMany에서 매핑되는 TmpItem Entity에서 참조할 필드가 없음

 

 

📰 해결 방법

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;
    }
}
 
 
 
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
31
import javax.persistence.*;
 
@Entity
public class TmpItem {
 
    @Id
    @GeneratedValue
    @Column(name = "TMP_ITEM_ID")
    private Long id;
 
    @ManyToOne
    @JoinColumn(name = "TMP_ID")
    private Tmp tmp;
 
    public Long getId() {
        return id;
    }
 
    public void setId(Long id) {
        this.id = id;
    }
 
    public Tmp getTmp() {
        return tmp;
    }
 
    public void setTmp(Tmp tmp) {
        this.tmp = tmp;
    }
}
 
 
 

 

TmpItem Entity에 매핑된 필드와 동일한 이름으로 변경

     - @OneToMany(mappedBy = "temp") ▶ @OneToMany(mappedBy = "tmp")