java - Hibernate One-to-Many Mapping Using Annotations; I'm trying to use foreign key association to save a user id in another table. -


i'm not able make work. i've tried using join table, result same. user id need appear in table commissions doesn't.

here's how i've created entities.

for user , userrole i've used join table , works. i've tried same commission no success (the joining table remained empty) tried below foreign key association.

user:

@entity @table(name="users") public class user implements serializable{  /**  * serialversionuid  */ private static final long serialversionuid = 1l;  @id @generatedvalue(strategy=generationtype.identity) private integer id;  @notempty @column(name="username", unique=true, nullable=false) private string username;  @notempty @column(name="password", nullable=false) private string password;  @notempty @column(name="first_name", nullable=false) private string firstname;  @notempty @column(name="last_name", nullable=false) private string lastname;  @notempty @column(name="email", nullable=false) private string email;  @notempty @manytomany(targetentity=userrole.class, fetch = fetchtype.lazy) @jointable(name = "users_user_role",           joincolumns = { @joincolumn(name = "user_id") },           inversejoincolumns = { @joincolumn(name = "user_role_id") }) private set<userrole> userroles = new hashset<userrole>();  @onetomany(fetch = fetchtype.lazy, mappedby="user")  private set<commission> commissions;  //getters , setters 

commission:

@entity @table(name="commissions") public class commission implements serializable{  /**  * serialversionuid  */ private static final long serialversionuid = 1l;  @id @generatedvalue(strategy=generationtype.identity) private integer id;  @notempty @column(name="order_name", unique=true, nullable=false) private string ordername;  @notempty @column(name="order_details", unique=true, nullable=false) private string orderdetails;  @column(name="order_status", unique=true, nullable=false) private string orderstatus;  @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "user_id", nullable = false) private user user;  //getters , setters 

userrole:

@entity @table(name="user_role") public class userrole implements serializable{  /**  * serialversionuid  */ private static final long serialversionuid = 1l;  @id @generatedvalue(strategy=generationtype.identity) private integer id;   @column(name="role", length=15, unique=true, nullable=true) private string role = userroletype.user.getuserroletype();  // getuserroletype defined in enum 'admin', 'dba', 'user'  //getters , setters 

in userdao , commissiondao i've used sessionfactory save entities.

extract abstract class extended userdao , commissiondao.

@autowired private sessionfactory sessionfactory;  protected session getsession(){     return sessionfactory.getcurrentsession(); }  public void persist(t entity) {     getsession().save(entity); } 

when create new user, works fine. joining table has correct id's added it. however... when add new commission, commission added in commissions table user_id remains null.

i'm new hibernate , "project" of mine, think might've bit more can chew.

maybe issue somewhere else in code , not here?

anyhow, i'm in bit of bind , use expertise guys. cheers!

thanks rossi robinsion. it.

i missed associate user in commission controller. before.

@requestmapping(value = { "/newcommission" }, method = requestmethod.post) public string savecommission(@valid commission commission, bindingresult result, modelmap model) {      if (result.haserrors()) {         return "commissioncreation";     }      if(!commissionservice.isuserordernameunique(commission.getid(), commission.getordername())){         fielderror ordernameerror =new fielderror("commission","ordername",                  messagesource.getmessage("non.unique.ordername",                          new string[]{commission.getordername()}, locale.getdefault()));         result.adderror(ordernameerror);         return "commissioncreation";     }      commissionservice.savecommission(commission);      model.addattribute("success", "your commission created.");     model.addattribute("loggedinuser", getprincipal());     return "commissioncreationsuccess"; } 

this after.

@requestmapping(value = { "/newcommission" }, method = requestmethod.post) public string savecommission(@valid commission commission, bindingresult result, modelmap model) {      if (result.haserrors()) {         return "commissioncreation";     }      if(!commissionservice.isuserordernameunique(commission.getid(), commission.getordername())){         fielderror ordernameerror =new fielderror("commission","ordername",                  messagesource.getmessage("non.unique.ordername",                          new string[]{commission.getordername()}, locale.getdefault()));         result.adderror(ordernameerror);         return "commissioncreation";     }      user user = userservice.findbyusername(getprincipal()); // associated user properly.     commission.setuser(user);      commissionservice.savecommission(commission);      model.addattribute("success", "your commission created.");     model.addattribute("loggedinuser", getprincipal());     return "commissioncreationsuccess"; } 

i don't know how miss this... sigh...

thanks mate!


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -