表題が長いですが...。
やりたい事は、表題のままです。
例えば、こんなテーブル構造になっていたとします。
NetBeansの自動生成機能を利用するとこのようなオブジェクトになります。
@Entity
@Table(name = "SAMPLE_USER", catalog = "")
@Data
public class SampleUser implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected SampleUserPK sampleUserPK;
@Column(name = "UPDATE_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date updateDate;
}
@Embeddable
@Data
public class SampleUserPK implements Serializable {
@Basic(optional = false)
@Column(name = "ID", nullable = false)
private int id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "NAME", nullable = false, length = 100)
private String name;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 300)
@Column(name = "MAIL_ADDRESS", nullable = false, length = 300)
private String mailAddress;
}
このSampleUserPK#id
はPostgreSQLのSerial型なので、@GeneratedValue
を付与したくなります。
しかし残念ながら答えは、分かりませんでしたorz...
なので、@EmbeddedId
を外して@IdClass
で対応しました。
正解が知りたいです...。
@Entity
@Table(name = "SAMPLE_USER", catalog = "", schema = "main")
+@IdClass(SampleUserPK.class)
+@Data
public class SampleUser implements Serializable {
private static final long serialVersionUID = 1L;
- @EmbeddedId
+
+ @Id
+ @Basic(optional = false)
+ @Column(name = "ID", nullable = false)
@GeneratedValue(strategy = GenerationType.IDENTITY)
- protected SampleUserPK sampleUserPK;
+ private int id;
+ @Id
+ @Basic(optional = false)
+ @NotNull
+ @Size(min = 1, max = 100)
+ @Column(name = "NAME", nullable = false, length = 100)
+ private String name;
+ @Id
+ @Basic(optional = false)
+ @NotNull
+ @Size(min = 1, max = 300)
+ @Column(name = "MAIL_ADDRESS", nullable = false, length = 300)
+ private String mailAddress;
+
@Column(name = "UPDATE_DATE")
@Temporal(TemporalType.TIMESTAMP)
private Date updateDate;
public SampleUser() {
}
}
@Embeddable
@Data
public class SampleUserPK implements Serializable {
- @Basic(optional = false)
- @Column(name = "ID", nullable = false)
private int id;
- @Basic(optional = false)
- @NotNull
- @Size(min = 1, max = 100)
- @Column(name = "NAME", nullable = false, length = 100)
+
private String name;
- @Basic(optional = false)
- @NotNull
- @Size(min = 1, max = 300)
- @Column(name = "MAIL_ADDRESS", nullable = false, length = 300)
+
private String mailAddress;
public SampleUserPK() {
}
}