java - Hibernate - OneToMany Bidirectional Insert resulting in null -
so, i'm making webapp using hibernate 5 , springmvc 4. can't, reason, insert entity onetomany relation. before explain first want tried many of solutions posted here , other forums , neither worked me... don't know if i'm doing wrong while trying solve issue.
now, added phone table. before this, add profile object , cascade inserts of address , user entities on same transaction. now, addition of phone entity code persist operation keeps throwing me
column 'profileid' cannot null
all classes have respective setters , getters... keep code short, removed them.
@entity @table(name = "profile") public class profile { @id @generatedvalue(strategy = generationtype.identity) private long id; @notempty @size(max = 100) @column(name = "firstname", nullable = false, unique = false, length = 100) private string firstname; @notempty @size(max = 100) @column(name = "lastname", nullable = false, unique = false, length = 100) private string lastname; @notnull @datetimeformat(pattern = "mm/dd/yyyy") @column(name = "birthdate", nullable = false, unique = false) private timestamp birthdate; @valid @onetoone(fetch = fetchtype.eager, cascade = cascadetype.all) @joincolumn(name = "userid") private user user; @valid @onetoone(fetch = fetchtype.eager, cascade = cascadetype.all) @joincolumn(name = "addressid") private address address; @valid @onetomany(fetch = fetchtype.eager, mappedby = "profile", cascade = cascadetype.all) private list<phone> phones; }
_
@entity @table(name = "phone") public class phone { @id @generatedvalue(strategy = generationtype.identity) private long id; @notempty @size(max = 50) @column(name = "phonenumber", nullable = false, unique = false, length = 50) private string phonenumber; @valid @manytoone(cascade = cascadetype.all, optional = false) @joincolumn(name = "profileid", nullable = false) private profile profile; }
_
@entity @table(name = "\"user\"", uniqueconstraints = { @uniqueconstraint(columnnames = "username") }) public class user { @id @generatedvalue(strategy = generationtype.identity) @column(name = "id", unique = true, nullable = false) private long id; @column(name = "username", nullable = false, unique = true, length = 80) private string username; @column(name = "password", nullable = false, length = 128) private string password; @valid @onetoone(fetch = fetchtype.lazy, mappedby = "user", cascade = cascadetype.all) private profile profile; }
_
@entity @table(name = "address") public class address { @id @generatedvalue(strategy = generationtype.identity) private long id; @column(name = "fulladdress", nullable = false, unique = false, length = 100) private string fulladdress; @column(name = "city", nullable = false, unique = false, length = 100) private string city; @column(name = "postalcode", nullable = false, unique = false, length = 100) private string postalcode; @onetoone(fetch = fetchtype.lazy, mappedby = "address", cascade = cascadetype.all) private profile profile; }
between solutions found similar cases was:
- to change profileid column of phone table make allow null values, keep nullable = false on joincolumn annotation. result inserted entities, profileid saved null.
- changed cascade types... still same error.
- someone said entitymanager contexts colliding.. not case.
- set joincolumn annotation attribute nullable false. did, still on code , not working either.
- set cascade type on both sides of relationship. did, still on code , not working.
- set manytoone relationship attribute optional false. not working.
being new i'm in this.. understand profileid receiving null because reason, after insert of profile table, generated id not yet set on profile object, causes phone insert fails. have no idea how solve.
in case need know how persisting objects...
sessionfactory.getcurrentsession().persist(profile);
and sessionfactory autowired. profile object has like:
{"phones":[{"phonenumber":"123456789"}],"user":{"username":"nameeeeee@alksjd.com","password":"123123123"},"firstname":"nameeeeee","lastname":"nameeeeee","birthdate":"02/05/2016", "address":{"fulladdress":"laksjdlkas","city":"alksjdlkasjd","postalcode":"101010"}}
and lastly full error:
hibernate: insert address (fulladdress, city, postalcode) values (?, ?, ?) hibernate: select last_insert_id() hibernate: insert `user` ( password, username ) values (?, ?) hibernate: select last_insert_id() hibernate: insert profile (addressid, birthdate, firstname, lastname, userid) values (?, ?, ?, ?, ?) hibernate: select last_insert_id() hibernate: insert phone (phonenumber, profileid) values (?, ?) warn sqlexceptionhelper::logexceptions:127 - sql error: 1048, sqlstate: 23000 error sqlexceptionhelper::logexceptions:129 - column 'profileid' cannot null
adding requested code.
@configuration @enabletransactionmanagement @componentscan(basepackages = { "com.configuration" }) @propertysource(value = { "classpath:application.properties" }) public class hibernateconfiguration { final static logger logger = logger.getlogger(hibernateconfiguration.class); @autowired private environment environment; @bean public localsessionfactorybean sessionfactorybean() { logger.info("creating localsessionfactorybean..."); localsessionfactorybean sessionfactorybean = new localsessionfactorybean(); sessionfactorybean.setdatasource(datasource()); sessionfactorybean.setpackagestoscan(new string[] { "com.model" }); sessionfactorybean.sethibernateproperties(hibernateproperties()); return sessionfactorybean; } @bean public datasource datasource() { drivermanagerdatasource datasource = new drivermanagerdatasource(); datasource.setdriverclassname(environment.getrequiredproperty("jdbc.driverclassname")); datasource.seturl(environment.getrequiredproperty("jdbc.url")); datasource.setusername(environment.getrequiredproperty("jdbc.username")); datasource.setpassword(environment.getrequiredproperty("jdbc.password")); return datasource; } @bean @autowired public hibernatetransactionmanager transactionmanager(sessionfactory sessionfactory) { hibernatetransactionmanager transactionmanager = new hibernatetransactionmanager(); transactionmanager.setsessionfactory(sessionfactory); return transactionmanager; } private properties hibernateproperties() { properties properties = new properties(); properties.put("hibernate.dialect", environment.getrequiredproperty("hibernate.dialect")); properties.put("hibernate.show_sql", environment.getrequiredproperty("hibernate.show_sql")); properties.put("hibernate.format_sql", environment.getrequiredproperty("hibernate.format_sql")); properties.put("hibernate.temp.use_jdbc_metadata_defaults", environment.getrequiredproperty("hibernate.temp.use_jdbc_metadata_defaults")); }
somewhere in code should this
phone phone = new phone(); //... set phone vars phone.setprofile(profile); sessionfactory.getcurrentsession().persist(profile);
Comments
Post a Comment