java - @OneToOne Hibernate with annotations. Can't properly save -
i can't make foreign keys auto generate using hibernate , jpa annotations. seems ok, entries saved in database. date come 1 form which, when submited creates user object modelattribute , saves in database. here beans. else should add ?
@entity @table(name="adress") public class adress implements serializable { @id @generatedvalue(strategy=generationtype.auto) @column(name="adress_id") private integer adressid; @notblank(message="the city must completed") @column(name="city") @size(min=5,max=30) private string city; @notblank(message="the street must completed") @column(name="street") @size(min=5,max=30) private string street; @notnull(message="the street number must completed") @numberformat @column(name="street_no") private integer streetno; @onetoone @joincolumn(name="user_id") private user user;}
and other one:
@entity @table(name="users") public class user implements serializable { @id @column(name="user_id") @generatedvalue(strategy=generationtype.auto) private integer userid; @notblank(message="username can't blank") @size(min=5,max=30) @column(name="username") private string username; @notblank(message="password field can't blank") @size(min=5,max=30) @column(name="password") private string password; @numberformat @notnull(message="age field must not blank") @column(name="age") private integer age; @column(name="message") @size(min=0,max=100) private string message; @column(name="date") @datetimeformat(pattern="dd/mm/yyyy") private date datecreated; @onetoone(mappedby="user",cascade=cascadetype.all,fetch=fetchtype.eager) private adress adress;
+getters , setters them
public void save(t entity){ sessionfactory.getcurrentsession().save(entity); }
if understand correctly , you're trying hibernate set foreign key on related record might help. try getting rid of mappedby , instead specify joincolumn. works me on 1 many:
the order:
@entity @table(name = "`order`") public class order implements serializable { @id @generatedvalue private long id; // order columns... @onetomany(cascade = cascadetype.all) @joincolumn(name = "order_id") private set<item> items; }
the item:
@entity @table(name = "item") public class item implements serializable { @id @generatedvalue private long id; // item columns... @manytoone(optional = false) @joincolumn(name = "order_id", referencedcolumnname = "id", nullable = false) private order order; }
Comments
Post a Comment