Community
Participate
Working Groups
When generating an entity from a table with automatic id generation for the primary key ID, the generated entity lacks the @GeneratedValue(strategy=GenerationType.IDENTITY) annotation. The result of this is that the id returned to the persisted entity is not updated (via a select last_insert_id()) and defaults to 0. Any use of this id by a child entity (via a foreign key mapping) will fail. Workaround: After generation, manually add @GeneratedValue annotation to all parent entities. code: public static void main(String[] args) { em.getTransaction().begin(); aSite = dao.createSite("aSite"); em.getTransaction().commit(); System.out.println("Site id: " + aSite.getId()); } DAO.java public Site createSite(String name) { Site aSite = new Site(); aSite.setName(name); em.persist(aSite); return aSite; } Schema: create table job (ID integer(19) AUTO_INCREMENT,PRIMARY KEY (ID)); Generated Entity: @Entity public class Site implements Serializable { @Id private int id; private String name; ..... should be: @Entity public class Site implements Serializable { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private int id; private String name; ..... or we will get the following: [TopLink Fine]: Connection(8140933)--INSERT INTO SITE (ID) VALUES (?) bind => [0] Site id: 0 and an SQL error for any child of [Site] like Site<-Job where the fk is 0 instead of the generated Site id: Internal Exception: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`jpa/job`, CONSTRAINT `fk_site_job_id` FOREIGN KEY (`job_site`) REFERENCES `site` (`ID`))Error Code: 1452 Call:INSERT INTO JOB (ID, job_site) VALUES (?, ?) bind => [0, 0] Query:InsertObjectQuery(com.ejb4.business.entity.Job@134ce4a) at oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:295) With the @GeneratedValue annotation (see ch4 p.88 of Pro EJB 3 JPA book by Mike Keith) we get back a populated ID field on the persisted Site: [TopLink Fine]: Connection(3299256)--INSERT INTO SITE (NAME) VALUES (?) bind => [aSite] [TopLink Fine]: Connection(3299256)--SELECT LAST_INSERT_ID() Site id: 45 WindowsXPSP2 Dali 0.5.0.20060621 This may be an underlying essentials generation problem if dali defers annotation generation to toplink - which i understant it does not.
Is it possible to raise the priority on this and get it addressed for Dali 2.1? Do we need support from data tools in their models so that ID columns from a given database connection's table object can be identified as auto-generated?
It appears that there is some API in DTP that could be useful for this but would be hard to tell without trying it out (given the lack of documentation). Column.getIdentitySpecifier() 2.1 is already closed for new features (RC1 is this week). We are actually planning on doing a large overhaul of Entity Generation in 2.2, so it is possible that this could be looked at as a part of that effort. I'll go ahead and target for 2.2 to be sure it is considered.
Danny, Can you look into the feasibility of adding this to Entity generation, or perhaps there is something like this already in the new Entity Gen?
This bug is same as the problem we discussed in Bug 249658. The fix requires change in DTP API to exposed a method isAutoIncrement(). We have submitted bug 250023 to DTP and wait for fix.
Ran out of time for this in 2.2.
Moving JPA specific bugs to new JPA component in bugzilla.