Java/JPA with Error

[해결 방법] org.hibernate.AnnotationException

HJ0216 2023. 8. 16. 12:46

👉 기본 환경

- 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 {
 
    @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
Exception in thread "main" org.hibernate.AnnotationException: No identifier specified for entity: jpabook.jpashop.domain.Tmp
    at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:266)
    at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:781)
    at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:254)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:230)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:273)
    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 선언 시, 필수적으로 @Id를 통해서 PK 1개 이상 지정해야 함

⭐ 데이터베이스 무결성 - 키 무결성

    - [CS] Database

 

 

📰 해결 방법

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;
    }
}
 
 
 

PK에 @Id 추가