sql server - hibernate - allow null value on FK -
i have 2 tables associated fk.
table student mapped this:
@entity @table(name="student") public class student implements serializable { ... @id @generatedvalue private int id; @manytoone(fetch = fetchtype.lazy, optional = true) @joincolumn(name = "school_id", nullable = true, insertable = false, updatable = false) private school school; private integer school_id; @transient private boolean editable = false; }
table school:
@entity @table(name="school") public class school implements serializable { ... @onetomany(fetch = fetchtype.lazy, mappedby = "school") private set<student> student = new hashset<student>(0);
when try insert/update student, isn't @ school (student.school_id null
) reports:
exception: java.lang.exception: org.hibernate.exception.constraintviolationexception: not update: [tables.student#556758] ... caused by: com.microsoft.sqlserver.jdbc.sqlserverexception: update statement conflicted foreign key constraint "fk__student__school_id__57378e7f". conflict occurred in database "db", table "dbo.school", column 'id'.
do have possibility insert null
values on fk?
shall define on:
- entity level or
- database level?
update:
i've change private school school = new school()
, when try insert/update row, still reports:
severe: servlet.service() servlet [faces servlet] in context path [/db] threw exception [javax.el.propertynotfoundexception: /view.xhtml @183,102 value="#{item.school.id}": target unreachable, 'school' returned null] root cause javax.el.propertynotfoundexception: /view.xhtml @183,102 value="#{item.school.id}": target unreachable, 'school' returned null
view.xhtml:
<rich:column> <h:outputtext value="#{item.school != null ? item.school.name : null}" rendered="#{!item.editable}"/> <h:selectonemenu id="som" tabindex="1" value="#{item.school.id}" rendered="#{item.editable}"> <f:selectitems value="#{mydials.schoollist}"/> </h:selectonemenu> </rich:column> <rich:column>
you mapped 2 different fields on same school_id
column:
private school school; private integer school_id;
remove school_id field. don't need it, since have association school entity.
and remove insertable = false, updatable = false
association mapping. should use school
field create, update or remove association.
Comments
Post a Comment