java - insert issue with auto-increment ID with Hibernate for a many to many association -
i use hibernate. db has many many relationship. hibernate generated these 2 classes handle relationship:
public class exducours implements java.io.serializable, iembededimproitem { private static final long serialversionuid = 1017414l; private exducoursid exducoursid; private coursimpro coursimpro; private eximpro eximpro; ... @embeddedid @attributeoverrides({ @attributeoverride(name = "coursid", column = @column(name = "cours_id", nullable = false) ), @attributeoverride(name = "exid", column = @column(name = "ex_id", nullable = false) ), @attributeoverride(name = "duree", column = @column(name = "duree") ), @attributeoverride(name = "commentaire", column = @column(name = "commentaire") ), @attributeoverride(name = "id", column = @column(name = "id", unique = true, nullable = false) ) })
and:
@embeddable public class exducoursid implements java.io.serializable { private static final long serialversionuid = 10198474l; private int coursid; private int exid; private integer duree=0; private string commentaire; private int id; id annotated way: @generatedvalue(strategy=generationtype.identity) @column(name = "id", unique = true, nullable = false) public int getid() { return this.id; }
if try save instance, hibernate refuse because not recognize id field identity. can understand @attributeoverrides not specify @generatedvalue .
i did not find anyway set id identity value: @attributeoverride(name = "id", column = @column(name = "id", unique = true, nullable = false) ) if there way so, solution problem.
the alternate solution thought move id field class exducoursid exducours... made appropriate changes way:
public class exducours implements java.io.serializable, iembededimproitem { private static final long serialversionuid = 1017414l; private exducoursid exducoursid;generated hibernate, contains parameters private coursimpro coursimpro; private eximpro eximpro; private int id; //added id field here!!
i commented both lines: //@embeddedid // @attributeoverride(name = "id", column = @column(name = "id", unique = true, nullable = false) )
i removed references id in class exducoursid.
my demo stops fails error: error: insert statement conflicted foreign key constraint "fk_ex_du_cours_cours_impro". conflict occurred in database "improdb", table "dbo.cours_impro", column 'id'
can think solution? there must 1 setupping many many relationship via hibernate must extremely common.
thx in advance.
Comments
Post a Comment