6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

@EmbeddedIdによって埋め込まれた@Embeddableな複合PKのうち一つのフィールドを@GenereatedValueしたい(暫定対応)。

Posted at

表題が長いですが...。

やりたい事は、表題のままです。
例えば、こんなテーブル構造になっていたとします。

kobito.1401795133.583506.png

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() {
     }
}

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?