Nov 18th, 2010, 02:08 PM
DBRE problems: Unique field constraint, FK on non-PK field, NOT NULL FKs
I am having difficult times with DBRE.
In order not to spam the forum with multiple questions here is one post with several questions:
1. If I have unique field "username" declared in table "person", it doesn't get translated to the generated model, namely Person class contains:
@Column(name = "username")
private Integer Person.username;
In other words, there is no unique=true inside @Column annotation
Is this a bug?
2. Now, lets say I have another table called "comment" which has foreign key "username" referencing unique field "username" in "person" table.
Note, that I deliberately made foreign key referencing non primary key because of the limitation with user generated primary-keys. explained here: http://forum.springsource.org/showthread.php?t=84078
But surprisingly the generated Person class doesn't contain username field!?
When I run tests for comments they fail as expected with:
Caused by: org.hibernate.MappingException: Unable to find column with logical name: username in org.hibernate.mapping.Table(person_db.person) and its related supertables and secondary tables.
Is this a bug?
3. Now lets say I changed the foreign key in "comment" table so it references id and not username in "person" table.
Although user_id field in "comment" table is specified as NOT NULL,
the generated Comment class has:
@JoinColumn(name = "user_id", referencedColumnName = "id")
private Person Comment.person;
In other words NOT NULL is not added as annotation.
The consequence is:
2010-11-18 19:38:16,831 [main] ERROR org.hibernate.util.JDBCExceptionReporter - Column 'user_id' cannot be null
Then I am stucked.
If I manually add @NotNull in CommentDbManaged.aj, that doesn't help, since TestDataOnDemand would not be changed automatically.
Is NOT NULL on FK supported at all or I am missing something?
4. Finally, when I put DEFAULT NULL to all my FKs (not something I am happy with), all integration tests pass.
Thanks for help in advance,
Nov 18th, 2010, 03:22 PM
Please raise a Jira improvement ticket on the unique field name question - I may be able to do something here. I do add @NotNull to non-relationship fields. I can extend this. Raise another request for this as well. Please do your testing off the latest code from Git. I have made significant changes since 1.1.0.RELEASE including a number of bug fixes.
For Q2, this could be a bug, but will need to see your project.
DBRE aims to produce legal JPA 2 annotated fields. If you believe there is an error in the JPA annotations created by Roo, then they need to be fixed. Unfortunately, I can't control what Hibernate does after this. EclipseLink and OpenJPA seem to do a better job here. Have you tried these?
On the first Jira issue, attach your database's DDL, and do a backup of the project (Roo backup command) and attach it as well. I need to be able to test exactly what you have. If it is too big, send it to stewarta at vmware dot com
Last edited by Alan Stewart; Nov 18th, 2010 at 03:42 PM.
Nov 18th, 2010, 06:21 PM
thanks very much.
I've created 3 issues:
1. Unique constraint not handled
2. FK NOT NULL constraint not handled
3. FK referencing non PK field - strange things happen