Bug 195903 - Entity Generation - table with auto_increment ID does not generate @GeneratedValue annotation
Summary: Entity Generation - table with auto_increment ID does not generate @Generated...
Status: NEW
Alias: None
Product: Dali JPA Tools
Classification: WebTools
Component: JPA (show other bugs)
Version: 1.0   Edit
Hardware: PC Windows XP
: P3 enhancement (vote)
Target Milestone: Future   Edit
Assignee: Danny Ju CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
Depends on: 249658
Blocks:
  Show dependency tree
 
Reported: 2007-07-09 16:58 EDT by Michael OBrien CLA
Modified: 2011-07-01 16:26 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael OBrien CLA 2007-07-09 16:58:10 EDT
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.
Comment 1 Leonard Theivendra CLA 2008-11-30 15:21:16 EST
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?
Comment 2 Neil Hauge CLA 2008-12-01 11:14:28 EST
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.
Comment 3 Neil Hauge CLA 2009-03-31 17:07:44 EDT
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?
Comment 4 Danny Ju CLA 2009-04-13 18:49:16 EDT
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.
Comment 5 Neil Hauge CLA 2009-05-28 13:34:05 EDT
Ran out of time for this in 2.2.
Comment 6 Neil Hauge CLA 2011-07-01 16:26:17 EDT
Moving JPA specific bugs to new JPA component in bugzilla.