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.
- 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, ...
Agreed, this seems necessary.
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.
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.
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.
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.
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.
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.
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.
Ok, then hopefully we can generate optional=false for 6.5.
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.
For my purposes, the Basic annotation is not necessary and I'm fine with leaving the Column annotation as it is.
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.
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.
Since we do not generate nullable for @JoinColum either, it make sense not to have it for @Column.
Checked in http://hg.netbeans.org/main/rev/a265cb996e0f to generate optional=false for @Basic and remove nullable=false from @Column.
Thanks.
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.
Not sure why it stops working. Will debug into it.
More fix - http://hg.netbeans.org/main/rev/fcc2fbf4fbb6
The nullable on the @Column will also be generated if the "Attributes for Regenerating Schema" is checked in the last step of the wizard.