ID saved correctly but returns 0 [closed]

2

I have a question I wanted to know what would return the returnz , why I wanted the method to return the ID of the inserted CD but it does not and I do not understand it.

The ID is automatically generated in the database and my problem is that if I ask for the id I get that it is 0, but then it is saved well in the database. I do not understand, why does that happen?

This is my code:

public int insertCd(Cd cd) {
    Integer returnz = null;        
    try {
        iniciaOperacion();//
        //tx = session.beginTransaction();
        returnz = (Integer) session.save(cd);
        tx.commit();        
    } catch (HibernateException ex) {
        if (tx != null)//
            tx.rollback();
        System.out.println("Problem creating session factory");
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return returnz;
}

private void iniciaOperacion() throws HibernateException {
    session = HibernateDAO.getSessionFactory().openSession();
    tx = session.beginTransaction();    
}
@Entity
@Table(name = "cd")

public class Cd {

    @Id
    @Column(name = "id", nullable = false)
    private int id; //Id of the cd

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

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

    @Column(name="any")
    private int any;

    public  Cd(){}


    public Cd(int id, String titol, String autor, int any) {
        this.id = id;
        this.titol = titol;
        this.autor = autor;
        this.any = any;
    }
    
asked by Iron Man 11.07.2017 в 11:52
source

0 answers