Total Pageviews

Sunday 18 March 2012

Get Auto Increment Id after persist

If you have entity marked with annotations @Id and @GeneratedValue(strategy = GenerationType.AUTO), you want get the Id after persist(), then use em.flush().

public int addEmployer(String name) {
        Employer e1 = new Employer();       
        e1.setName(name);
        em.persist(e1);
        em.flush();       
        return e1.getId();
    }

No comments:

Post a Comment