0

I have few columns in table Journal of which JournalKey is Primary key and JournalID has Unique Constraint.

JournalKey is autogenerated Key.

Now when i start saving using insertOrUpdate, i have expected hibernate to Insert the row when JournalID is not present else update it.

Since JournalKey is autogenerated it doesn't have much role to play. However it gives me UniqueConstraintViolation error.

Kindly help.

Journal is save as

    public Object saveOrUpdateObj(Object obj){
    Session session = HibernateUtil.getSession();
    try{
        session.beginTransaction();
        session.saveOrUpdate(obj);
        session.getTransaction().commit();
    }finally{
        session.close();
    }
    return obj;
}

Journal class is defined this way

@Entity
@Table(name="Dim_Journal")
public class Journal {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="JournalKey")
private Long journalKey;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="FK_Org_Key")
private Organisation organisation;

@Column(name="JournalID")
private String journalID;

@Column(name="JournalDate")
private Date journalDate;

@Column(name="JournalNumber")
private String JournalNumber;

@Column(name="JournalCreationDate")
private Date createdDateUTC;

@Column(name="SourceID")
private String SourceID;

@Column(name="SourceType")
@Enumerated(EnumType.STRING)
private JournalSourceType sourceType;

@Transient
private List<JournalLine> journalLines;


@Transient
private Long organisationId;

}
saurabh jain
  • 167
  • 1
  • 3
  • 12
  • 1
    But `journalKey` is the primary key, the value of JournalID is not relevant in this case. Unless I am missing the point. Please post the whole code where you persist the entity – rapasoft May 12 '16 at 06:58
  • @rapasoft: code added – saurabh jain May 12 '16 at 07:32
  • What version of Hibernate. – Alan Hay May 12 '16 at 07:49
  • Could you add code related to `@ManyToOne` relation? I suspect you are saving this object twice, since you have `CascadeType.ALL` there. Are you perhaps saving the `Organisation` somewhere before? – rapasoft May 12 '16 at 08:34
  • @rapasoft: Organisation is already saved in other function ,here i just get its reference and set it in Journal. – saurabh jain May 13 '16 at 07:40
  • Then the problem is with your `Organisation` entity. You have saved it already, and now, when you try to save `Journal` you get constraint error because you've set the cascade option on it (`@ManyToOne(cascade=CascadeType.ALL)`) – rapasoft May 14 '16 at 18:49

1 Answers1

0

How can you do auto generate String with AUTO algorithm in JPA?

This is wrong you are doing. If Id field is a String you can use UUID to generate the auto String, else make Id as Long type.

click here to see, How to implement String with UUID algo in JPA.

Community
  • 1
  • 1
sitakant
  • 1,766
  • 2
  • 18
  • 38
  • I have updated the journalKey to Long..but it still gives me the same error com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '1b58bc89-bd49-45b5-af57-7df8c42006a9' for key 'JournalID' – saurabh jain May 12 '16 at 07:27
  • Based on the exception, I concluded that , you have used UUID algorithm.I belief you have missed some configuration, hence you are generation only one UUID and storing it into DB. One time, It can store , but for the 2nd time with the same UUID you'll get the same exception. – sitakant May 12 '16 at 07:45