java - "Save transient object before flushing" error -
this question has answer here:
i tried save 1 of entity site containing user, user registered in database , don't want save too.
problem when try save site, following error :
org.springframework.dao.invaliddataaccessapiusageexception: object references unsaved transient instance - save transient instance before flushing: com.project.netmg.bo.impl.user; nested exception org.hibernate.transientobjectexception: object references unsaved transient instance - save transient instance before flushing: com.project.netmg.bo.impl.user i think, try save user it's not want. want search user , assign site.
java code :
public string savesite(@valid @modelattribute siteform siteform, bindingresult bindingresult, model uimodel) { populatesiteforsave(siteform); _siteservice.savesite(siteform.getsite()); return webconstants.redirect_to_view_site_url + siteform.getsite().getsitename(); } private void populatesiteforsave(siteform siteform) { siteform.getsite().setcountry((country) _countryservice.getcountrybyname(siteform.getsite().getcountry().getname())); siteform.getsite().setbusiness((business) _businessservice.getbusinessbyid(siteform.getsite().getbusiness().getid())); siteform.getsite().setstatus((status) _statusservice.getstatusbyid(siteform.getsite().getstatus().getid())); if (!siteform.getlocalitfullname().isempty()) { siteform.getsite().setlocalit(_userservice.finduserbyuserfullname(siteform.getlocalitfullname())); // user } else { siteform.getsite().setlocalit(null); } if (!siteform.getriffullname().isempty()) { siteform.getsite().setrif(_userservice.finduserbyuserfullname(siteform.getriffullname())); //user } else { siteform.getsite().setrif(null); } if (siteform.getsite().getlocalcontact().getid() != null) { siteform.getsite().setlocalcontact((user) _userservice.finduserbyusername(siteform.getsite().getlocalcontact().getusername())); //user } } site class:
@entity @table(name = "site", uniqueconstraints = { @uniqueconstraint(columnnames = { "site_country_id", "site_name" }) }) @audited(targetauditmode = relationtargetauditmode.not_audited, withmodifiedflag = true) public class site implements isite { /** constant serialversionuid. */ private static final long serialversionuid = -390717603276436784l; /** id. */ @id @generatedvalue(strategy = generationtype.auto) @column(name = "site_id", unique = true, nullable = false) private long id; /** site address. */ @column(name = "site_address", length = businessconstants.site_address) private string address; /** site analog phone number. */ @column(name = "site_analog_phone_number", length = businessconstants.site_analog_phone_number) private string analogphonenumber; /** site comment. */ @column(name = "site_comment", length = businessconstants.site_comment) private string comment; /** site entity code. */ @digits(integer = 3, fraction = 0, message = "please enter max 3 digits") @column(name = "site_entity_code", nullable = false) private long entitycode; /** site invoice code. */ @digits(integer = 10, fraction = 0, message = "please enter max 10 digits") @column(name = "site_invoice_code", nullable = false) private long invoicecode; /** site local phone. */ @column(name = "site_local_it_phone", length = businessconstants.site_local_it_phone) private string localitphone; /** site name. */ @notblank @column(name = "site_name", nullable = false, length = businessconstants.site_name) private string sitename; /** site subnet. */ @notblank @column(name = "site_subnet", nullable = false, length = businessconstants.site_subnet) private string subnet; /** site user number. */ @digits(integer = 4, fraction = 0, message = "please enter max 4 digits") @column(name = "site_user_number") private long usernumber; /** business. */ @valid @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "site_business_id", nullable = false) private business business; /** country. */ @valid @manytoone(fetch = fetchtype.eager) @joincolumn(name = "site_country_id") private country country; /** local contact. */ @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "site_local_contact", nullable = true) private user localcontact; /** local it. */ @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "site_local_it", nullable = true) private user localit; /** rif. */ @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "site_rif", nullable = true) private user rif; /** status. */ @valid @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "site_status_id", nullable = false) private status status; /** end date. */ @column(name = "site_end_date") @temporal(temporaltype.timestamp) @datetimeformat(iso = iso.date_time) private date enddate = null; @manytomany(targetentity = user.class, mappedby = "usersites", fetch = fetchtype.lazy) @notaudited private set<iuser> siteusers; user class :
@entity @table(name = "users") public class user implements iuser { /** constant serialversionuid. */ private static final long serialversionuid = 6741623705511494367l; private static final string user_id = "user_id"; /** user id. */ @id @generatedvalue(strategy = generationtype.auto) @column(name = user_id) private long id; /** user first name. */ @column(name = "firstname", length = businessconstants.user_firstname, nullable = false) @notnull private string userfirstname; /** user last name. */ @column(name = "lastname", length = businessconstants.user_lastname, nullable = false) @notnull private string userlastname; /** user email. */ @column(name = "email", length = businessconstants.user_email) @notnull private string useremail; /** user uid. */ @column(name = "login", length = businessconstants.user_login, nullable = false, unique = true) @notnull private string username; @valid @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "profile_id", referencedcolumnname = "prof_id", nullable = false) private profile profile; @batchsize(size = 20) @manytomany(cascade = { cascadetype.merge, cascadetype.persist }, fetch = fetchtype.lazy,targetentity=site.class) @jointable(name = "user_site", joincolumns = { @joincolumn(name = user_id, nullable = false) }, inversejoincolumns = { @joincolumn(name = "site_id", nullable = false) }) private set<isite> usersites; @manytomany(cascade = { cascadetype.merge, cascadetype.persist }, fetch = fetchtype.lazy,targetentity=region.class) @jointable(name = "user_region", joincolumns = { @joincolumn(name = user_id, nullable = false) }, inversejoincolumns = { @joincolumn(name = "region_id", nullable = false) }) private set<iregion> userregions; @batchsize(size = 20) @manytomany(cascade = { cascadetype.merge, cascadetype.persist }, fetch = fetchtype.lazy,targetentity=zone.class ) @jointable(name = "user_zone", joincolumns = { @joincolumn(name = user_id, nullable = false) }, inversejoincolumns = { @joincolumn(name = "zone_id", nullable = false) }) private set<izone> userzones; @batchsize(size = 20) @manytomany(cascade = { cascadetype.merge, cascadetype.persist }, fetch = fetchtype.lazy,targetentity=country.class ) @jointable(name = "user_country", joincolumns = { @joincolumn(name = user_id, nullable = false) }, inversejoincolumns = { @joincolumn(name = "country_id", nullable = false) }) private set<icountry> usercountries; @transient private collection<? extends grantedauthority> authorities; @transient private string userfullname;
you can't have both ways. if object graph includes user, (have to) persisted if changed in code. have considered evens means fetch site (including user), change localcontact , persist site again?
if want localcontact settable in object graph, not persisted, can annotated @transient:
/** local contact. */ @transient private user localcontact; hope helps.
cheers,
Comments
Post a Comment