java - Hibernate Model: set default @ManyToOne value of a Child Class -


i have class user has @manytoone field role.

@entity  @table(name="users") @inheritance(strategy = inheritancetype.single_table) @discriminatorvalue("role_admin") @discriminatorcolumn (name="rolename", discriminatortype= discriminatortype.string, length=20) public class user extends baseentity implements userdetails {     // others fields username, password,...      @manytoone     @joincolumn(name = "role_id", referencedcolumnname="id")      @notnull(message = "role field mandatory")     private role role;      //getter , setter } 

i have many classes extends user: userseller, userclient, useroffice....

@entity     @discriminatorvalue("role_seller")     @attributeoverride(name = "role", column = @column(name = "role_id"))     public class userseller extends user {         //additional fields companyid,...         //getter & setter     } 

i have 1 panel can insert / edit / delete kind of users, must have "n" panel: 1 each kind of user.

when i'm in these panel, able insert these userseller without having put choose role, want set role default.

• tried put costructor in userseller

@entity @discriminatorvalue("role_seller") @attributeoverride(name = "role", column = @column(name = "role_id")) public class userseller extends user {      @transient     roleservice roleservice;      @transient     user user = new user();       public userseller()     {         super();          try         {             this.setrole(roleservice.getrole("role_seller"));         }         catch (recordnotfoundexception e)         {          }     } 

but error:

could not constructor org.hibernate.persister.entity.singletableentitypersister 

• tried pass user object constructor:

public userseller(user user) 

and in controller this:

 user user = new user();  userseller seller = new userseller(user);  model.addattribute("seller", seller); 

but error:

no default constructor entity: com.machinet.model.uservenditore

  • if, in userseller declare again role field error "redefined column"....

  • finally found though solution (in userseller class):

    @prepersist public void prepersist() {         try         {             this.setrole(roleservice.getrole("role_seller"));         }         catch (recordnotfoundexception e)         {          }                } 

but when i'm in userseller panel , try add new seller, doesnt' take default role , validation fails.

i know how can trick: want userseller, userclient,... have default value when insert new record.

do need in controller? way? because beginner me, doesn't so elegant solution:

uservenditore venditore = new uservenditore(); try {     venditore.setrole(roleservice.getrole("role_venditore")); } catch (recordnotfoundexception ex) {  } model.addattribute("venditore", venditore); 

edit: last solution doesn't work: fails validation!

thank suggestion!

for now, found solution (in controllerclass):

  • in removed @valid before @modelattribute
  • i manually set role
  • i manually validate object

    @requestmapping(value = "/add", method = requestmethod.post) public string addingseller(@modelattribute userseller seller, bindingresult result, redirectattributes redirectattrs) {

    logger.info("in: seller/add-post");   //set role try {     seller.setrole(roleservice.getrole("role_seller")); } catch (recordnotfoundexception ex) {  }          //validation validator.validate(seller, result);  if (result.haserrors()) {     logger.info("seller-add error: " + result.tostring());     redirectattrs.addflashattribute("org.springframework.validation.bindingresult.seller", result);                 redirectattrs.addflashattribute("seller", seller);  } else {      try     {         sellerservice.addseller(seller);          //message user         string message = "seller added!";                    redirectattrs.addflashattribute("message", message);         redirectattrs.addflashattribute("message_class", "alert-success");           }     catch (duplicaterecordexception e)     {         //the username exists          //message user         string message = "already exists user username";                    redirectattrs.addflashattribute("message", message);         redirectattrs.addflashattribute("message_class", "alert-danger");                   redirectattrs.addflashattribute("seller", seller);     } }     return "redirect:/sellers/list"; 

    }


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -