This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 129869 - Entity Classes from Database does not generate nullable = false reliably
Summary: Entity Classes from Database does not generate nullable = false reliably
Status: RESOLVED FIXED
Alias: None
Product: javaee
Classification: Unclassified
Component: Persistence (show other bugs)
Version: 6.x
Hardware: All All
: P2 blocker (vote)
Assignee: Dongmei Cao
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-03-12 09:01 UTC by Matthew Bohm
Modified: 2008-06-10 01:06 UTC (History)
3 users (show)

See Also:
Issue Type: ENHANCEMENT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matthew Bohm 2008-03-12 09:01:28 UTC
- Set up MySQL.
- Set up the Sakila database (http://dev.mysql.com/doc/#sampledb)
- Run the Entity Classes from Database wizard on all tables except film_text, creating a persistence unit with "None"
for the table generation strategy. (You can put the generated entities in a package called "sakila".)
- Look at Inventory.java, for instance. It has the following field:

    @JoinColumn(name = "film_id", referencedColumnName = "film_id")
    @ManyToOne
    private Film filmId;

The JSF Pages from Entity Class wizard looks for a nullable = false on the @JoinColumn annotation. I believe it should
be there since the database column is not nullable:

CREATE TABLE inventory (
  inventory_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,
  film_id SMALLINT UNSIGNED NOT NULL,
...
Comment 1 Andrei Badea 2008-03-12 12:41:19 UTC
Agreed, this seems necessary.
Comment 2 Andrei Badea 2008-03-13 13:21:47 UTC
I have been thinking a bit more about this and I don't think it makes sense. My understanding is that the nullable
attribute is only used by the persistence provider to generate DDL to create the tables. It is not used when reading
data from database to create entities. As such, it is not needed in entity classes generated from database, since it is
unlikely that the user will want to recreate this tables from the generated entity classes.

I guess the wizard will have to manage without the annotation. Actually, the wizard needs to deal with existing entity
classes (not generated by the Entity Classes from DB wizard), and those are not guaranteed to have the annotation.
Comment 3 Matthew Bohm 2008-03-13 22:35:45 UTC
I see your logic. That said, the JSF pages wizard has a feature to generate required checks if the annotation is
present. If the party who creates the entities, whether computer or human, does not add the annotation, the required
checks will not be generated. So if the Entity Classes from Database wizard does not do it, the burden will fall on the
user to add any such annotations desired. The step of manually adding them will show up in evangelist demos and
tutorials. If we consider the JSF pages wizard to have a kind of "official" dependency on the Entity Classes from
Database wizard, then I think the latter should generate the annotations as a courtesy to the user. If we do not
consider such a dependency official, then I think it could be argued that the Entity Classes from Database wizard need
not generate the annotations.
Comment 4 Jayashri Visvanathan 2008-03-15 16:50:10 UTC
Andrei,
  just curious, how do you then enforce that the database column cannot be null, if there is no annotation ? Doesn't
persistance provider deal with that ?

Matt,
  My thought is if the annotation's only use case is JSF Entity pages wizard, then we probably shouldn't add it. To your
comment earlier, I think that the entity class from database shouldn't have a dependency on JSF pages from entity.
Comment 5 Matthew Bohm 2008-03-15 20:30:18 UTC
According to pbuzek, our evangelists like to use the JSF pages feature by running the Entities from DB wizard first, and
then running the JSF pages wizard on the entities. The less hand-coding they have to do in that process, the happier
they are. If people don't mind adding the nullable = false manually, then we can certainly table this for the next
release. But otherwise we should probably address it now.
Comment 6 Andrei Badea 2008-03-15 21:09:30 UTC
jayashri: very good question. The persistence provided uses the "optional" attribute, not the "nullable" one. I didn't
want to add the "nullable" attribute, because it is used for schema generation. Adding the "optional" one might make
more sense. However, "optional" is not without problems either. 

First, the the spec doesn't specify what the provider should do when a non-optional field is null. There is thus a
certain amount of non-portability associated with it, which could discourage users from using it. 

Second, the attribute has different semantics than "nullable". For example, it is only present on the Basic, ManyToOne
and OneToOne annotations. The JSF wizard would need to be updated too.
Comment 7 Matthew Bohm 2008-03-16 02:19:47 UTC
I don't see the harm in using nullable, even if it does do schema generation. Users may be unlikely to regenerate a
schema from the entities, but the option is theirs.

Incidentally, the Entities from DB wizard is already generating nullable = false, but only on non-relationship fields. I
don't see why we would not want to generate them on the relationship fields as well.
Comment 8 Andrei Badea 2008-03-20 14:00:58 UTC
Because nullable is not the right attribute to use -- optional is, as explained in desc3 and desc7. The fact that we
generate the nullable for @Column should probably be considered a bug.
Comment 9 Matthew Bohm 2008-04-29 23:25:36 UTC
Ok, then hopefully we can generate optional=false for 6.5.
Comment 10 Dongmei Cao 2008-05-16 20:39:40 UTC
Ok, I can enhance it to have optional=false for @ManyToOne and @OneToOne when the FK is not nullable.
Should we generate optional=false for @Basic when the field is not nullable?
I think we should leave the nullable=false for @Column in cases some people are actually using it.
Comment 11 Matthew Bohm 2008-05-16 20:59:32 UTC
For my purposes, the Basic annotation is not necessary and I'm fine with leaving the Column annotation as it is.
Comment 12 Dongmei Cao 2008-05-16 22:41:13 UTC
Checked in http://hg.netbeans.org/main/rev/78e3b9abf89f

Note: optional=false is not generated for @Basic when the column is not nullable. The code is commented out for now.
Please reopen the bug if you think that should be generated.
Comment 13 Andrei Badea 2008-05-19 15:49:39 UTC
So now the wizard generates:
- nullable for @Column
- optional for @ManyToOne and @OneToOne
- nothing for @Basic

That doesn't seem right, we should be consistent. Suggest dropping nullable for @Column and using optional everywhere.
Quite unlikely that users use nullable, since that is used when generating the tables from the entity classes. But users
don't do that, they already have the tables.
Comment 14 Dongmei Cao 2008-05-19 18:58:12 UTC
Since we do not generate nullable for @JoinColum either, it make sense not to have it for @Column.

Comment 15 Dongmei Cao 2008-05-19 18:58:47 UTC
Checked in http://hg.netbeans.org/main/rev/a265cb996e0f to generate optional=false for @Basic and remove nullable=false
from @Column. 
Comment 16 Andrei Badea 2008-05-23 15:33:04 UTC
Thanks.
Comment 17 Matthew Bohm 2008-06-04 18:11:43 UTC
From what I can tell, "optional = false" is not being generated for many-to-one relationships. Try the tutorial at
http://www.netbeans.org/kb/61/web/jsf-jpa-crud-wizard.html up through and including the section entitled "Generating the
Entity Classes from the Database."

Based on the current behavior, it would still be necessary to manually apply the "optional = false" annotation elements
to  the @ManyToOne annotations as shown in the next section of the tutorial (entitled "Editing the Entity Classes"). But
that is what we are trying to avoid.

Incidentally, there is only one non-nullable one-to-one relationship in the consulting agency database, namely between
Address and Client, and I observed that the wizard does generate an "optional = false" annotation element for that
relationship. So it appears the problem is only with many-to-one relationships.
Comment 18 Dongmei Cao 2008-06-04 23:51:36 UTC
Not sure why it stops working. Will debug into it.
Comment 19 Dongmei Cao 2008-06-05 03:23:15 UTC
More fix - http://hg.netbeans.org/main/rev/fcc2fbf4fbb6
Comment 20 Dongmei Cao 2008-06-10 01:06:53 UTC
The nullable on the @Column will also be generated if the "Attributes for Regenerating Schema" is checked in the last
step of the wizard.