2

I am trying to make an update on database using Hibernate. When i call merge, it results a new row in database. I don't receive any error. The cod is working, but i want update not insert.

Model:

@Entity
@Table(name = "Books")
public class Manual implements Serializable {
    @Id
    @GeneratedValue
    private int idManual;
    private String denumire;
    private String disciplina;
    private Date dataPostare;

    @Enumerated(EnumType.STRING)
    private TipManual tipManual;


    private String emailContact;
    private String telefonContact;
    private double pret;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "username")
    private User userName;

    public Manual() {

    }

    public Manual(int idManual, String denumire, String disciplina, Date dataPostare, TipManual tipManual,
            String emailContact, String telefonContact, double pret, User userName) {
        super();
        this.idManual = idManual;
        this.denumire = denumire;
        this.disciplina = disciplina;
        this.dataPostare = dataPostare;
        this.tipManual = tipManual;
        this.emailContact = emailContact;
        this.telefonContact = telefonContact;
        this.pret = pret;
        this.userName=userName;
    }

    public int getIdManual() {
        return idManual;
    }

    public void setIdManual(int idManual) {
        this.idManual = idManual;
    }

    public String getDenumire() {
        return denumire;
    }

    public void setDenumire(String denumire) {
        this.denumire = denumire;
    }

This is my model class, i also tried to remove @GeneratedValue for my id, but the problem is the same.

My function for merge:

@Override
    @Transactional
    public Manual updateBook(Manual book) {
        em.merge(book);
        em.flush();
        return book;
    }
4
  • Is there value in idManual of Manual entity when application calls updateBook ? Commented Jun 12, 2017 at 2:31
  • @adyjr, thx you are right Commented Jun 12, 2017 at 13:09
  • You are welcome @S.over17. If I Answer, do you mark my Answer as correct? Commented Jun 12, 2017 at 13:21
  • 1
    @adyjr, yes of course, just answer Commented Jun 12, 2017 at 14:42

2 Answers 2

1

Is there value in idManual of Manual entity when application calls updateBook?

Check this, because if identifier isn't present, the entity will be inserted again.

Sign up to request clarification or add additional context in comments.

Comments

0

if your entity is detached entity that's will happend , more info

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.