3

@EmbeddedId を使用しようとしています。これは次のような私のコードです。

create table TBL_EMPLOYEE_002(
  ID integer generated always as identity (start with 100,increment by 10), 
  COUNTRY varchar(50),
  NAME varchar(50),
  constraint PK_EMP_00240 primary key(ID,COUNTRY)
)

Embedded クラスは次のとおりです。

@Embeddable
public class EmployeeIdTwo implements Serializable{
     public EmployeeIdTwo(){}
    public EmployeeIdTwo(String country){
        this.empCountry = country;
    }

    @GeneratedValue(strategy=GenerationType.IDENTITY)
     @Column(name="ID") 
    private Integer employeeId;

    @Column(name="COUNTRY",length=50)
    private String empCountry;

// implementation of hashCode and equals and only getters 
...
}

次のような従業員エンティティ、

@Entity
@Table(name="TBL_EMPLOYEE_002")
public class EmployeeEntitySix implements Serializable{

    public EmployeeEntitySix(){}
    public EmployeeEntitySix(EmployeeIdTwo id,String name){
        this.id = id;
        this.employeeName = name;
    }

    @EmbeddedId    
    private EmployeeIdTwo id;

    @Column(name="NAME")
    private String employeeName;

// getters and setters
}

これは main メソッドに記述されたコードです。

private static void storVal(EntityManager em){
    EmployeeEntitySix employee = new EmployeeEntitySix(new EmployeeIdTwo("KENYA"), "Henry Olaanga");
    em.persist(employee);
}

しかし、上記のコードを実行すると、次のように例外が発生します。

Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Attempt to modify an identity column 'ID'. 

私のEmbeddedIdクラスに自動生成された列が含まれている場合、アプローチがどうあるべきかよりも、どこが間違っているのか教えてください。

永続化プロバイダーとして hibernate を使用し、永続化 API として JPA を使用していることを知っているだけです

4

1 に答える 1