JPA - ManyToOne - JoinTable - 无法更新字段

时间:2017-01-15 18:30:01

标签: java jpa jointable

我有以下情况:

AnagraficaIscritti.java

@Entity
@Table(name="ANAGRAFICA_ISCRITTI")
public class AnagraficaIscritti implements Serializable {
        private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="ID_EMAIL",insertable=false, updatable=false)
    private long idEmail;

    private String email;

    //bi-directional many-to-many association to Newsletter 
    @ManyToMany(mappedBy="anagraficaIscrittis")
    private List<Newsletter> newsletters;

    //bi-directional many-to-one association to IscrizioniEmail
    @OneToMany(mappedBy="anagraficaIscritti")
    private List<IscrizioniEmail> iscrizioniEmails;

Newsletter.java

@Entity
@Table(name="NEWSLETTER")
public class Newsletter implements Serializable {
        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="ID_NEWSLETTER")
        private long idNewsletter;

        //bi-directional many-to-many association to AnagraficaIscritti
        @ManyToMany
        @JoinTable(
            name="ISCRIZIONI_EMAIL"
            , joinColumns={
                @JoinColumn(name="ID_NEWSLETTER")
                }
            , inverseJoinColumns={
                @JoinColumn(name="ID_EMAIL")
                }
            )
        private List<AnagraficaIscritti> anagraficaIscrittis;

        //bi-directional many-to-one association to IscrizioniEmail
        @OneToMany(mappedBy="newsletter")
        private List<IscrizioniEmail> iscrizioniEmails;

IscrizioniEmail.java

@Entity
@Table(name="iscrizioni_email")
public class IscrizioniEmail implements Serializable {
        private static final long serialVersionUID = 1L;


        @EmbeddedId
        private IscrizioniEmailPK id;

        @Temporal(TemporalType.TIMESTAMP)
        @Column(name="SUBSCRIPTION_DATE")
        private Date subscriptionDate;

        //bi-directional many-to-one association to AnagraficaIscritti
        @ManyToOne
        @JoinColumn(name="ID_EMAIL",insertable = false, updatable = false)
        private AnagraficaIscritti anagraficaIscritti;

        //bi-directional many-to-one association to Newsletter
        @ManyToOne
        @JoinColumn(name="ID_NEWSLETTER", insertable = false, updatable = false)
        private Newsletter newsletter;

IscrizioniEmailPK.java

@Embeddable
public class IscrizioniEmailPK implements Serializable {
        //default serial version id, required for serializable classes.
        private static final long serialVersionUID = 1L;

        @Column(name="ID_EMAIL", insertable=false, updatable=false)
        private long idEmail;

        @Column(name="ID_NEWSLETTER", insertable=false, updatable=false)
        private long idNewsletter;

当我尝试创建Object AnagraficaIscritti和Object Newsletter时,也会在IscrizioniEmail中自动插入记录。例如,我创建:

 ANAGRAFICA_ISCRITTI
 ID_EMAIL           EMAIL
  1           john.doe@gmail.com

 NEWSLETTER
 ID_NEWSLETTER      NEWSLETTER
      1               sport

 ISCRIZIONI_EMAIL
  ID_EMAIL   ID_NEWSLETTER   SUBSCRIPTION_DATE
     1             1               NULL

但更新SUBSCRIPTION_DATE(使用当前时间戳)的正确方法是什么?如果我尝试创建一个Object IscrizioniEmail,我会收到重复的密钥错误,等等。

1 个答案:

答案 0 :(得分:0)

您可以在SUBSCRIPTION_DATE表格中为ISCRIZIONI_EMAIL设置一个默认值,该值将是当前日期。因此,您不需要从Java发送它。

如果您需要从Java发送该值,那么您可以在插入IscrizioniEmailNewsletter表后立即使用AnagraficaIscritti实体类执行更新查询。