How could I get the id of the last record inserted?
I have a child table where I insert a record and would like to know if there is a way to make directly after the insert an update on the father table.
How could I get the id of the last record inserted?
I have a child table where I insert a record and would like to know if there is a way to make directly after the insert an update on the father table.
Lol!!!
I sant know of this is possibile with orm or i should use transaction
Are you inserting the record on the child table with SQL, HQL or via entitySave()?
Jason Durham
I use entitysave()
Why do you need the ID at all? Let Hibernate handle it. This code snippet is assuming that user could have many roles.
var user = entityNew(“user”);
var role = entityNew(“role”);
user.addRoles(role);
role.setUser(user);
entitySave(user);
entitySave(role);
If this isn’t helpful, please post a code snippet so I can better understand the question.
Jason Durham
I have tried something similar, but not working!
I should set inverse=true?
What does “not working” mean? If you’re getting an error, post it.
Jason Durham
This is the error: object references an unsaved transient instance - save the transient instance before flushing: GAprofiles
Code and a stack trace is critical when asking for help.
I got this tables:
And where does GAprofiles come into this mix?
Jason Durham
forget Gaprofiles…
in this new example the code is correct?
The answer is in Barney’s blog post. I’m not sure why you need us to read it for you. This also has nothing to do with Coldbox.
If you want help with your problem, post real code, exception and stacktrace.
Jason Durham
this is my entity GAprofiles
/**
// Primary Key
property name=“id” fieldtype=“id” generator=“native”;
//Relationship
property name=“Email” fieldtype=“one-to-many” cfc=“Email” fkcolumn=“profilo” inverse=“true”;
// Properties
/*
*@Display Nome
*@NotEmpty
*/
property name=“nome” ormtype=“string”;
property name=“token” ormtype=“string”;
property name=“refresh_token” ormtype=“string”;
property name=“token_expiry” ormtype=“timestamp”;
property name=“profilo” ormtype=“string”;
// Constructor
function init(){
return this;
}
}
This is entity Email
/**
// Primary Key
property name=“id” fieldtype=“id” generator=“native”;
//Relationship
property name=“GAprofiles” fieldtype=“many-to-one” fkcolumn=“profilo” cfc=“GAprofiles” inverse=“true”;
property name=“sito” fieldtype=“one-to-many” fkcolumn=“emailId” cfc=“Sito”;
// Properties
property name=“email” ormtype=“string”;
property name=“recupero” ormtype=“string”;
property name=“password” ormtype=“string”;
//Validation
this.constraints = {
email = {required=true},
password = {required=true}
};
// Constructor
function init(){
return this;
}
}
I’m trying to create a new Gaprofiles and add to an existing email
function test(event,rc){
p = EntityLoadByPK(“Email”, 1);
p.setEmail(“Barney”);
c = profilesService.new(ENTITYNAME=“gaprofiles”);
c.setProfilo(“cat”);
c.addEmail§;
p.setGAprofiles©;
//writeDump©;writeDump§;abort;
emailService.save§;
profilesService.save©;
getMyPlugin(“MessageBox”).info(“Profilo inserito correttamente nel database!”);
setNextEvent(“profiles.index”);
}
This is error:
Error Messages: object references an unsaved transient instance - save the transient instance before flushing: GAprofiles
Root cause :org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: GAprofiles
This is Stack Trace:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: GAprofiles
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:244)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:449)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:296)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:668)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3380)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:520)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:230)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:154)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at coldfusion.orm.hibernate.SessionWrapper.flush(SessionWrapper.java:176)
at coldfusion.orm.hibernate.HibernateSessionManager.flushSession(HibernateSessionManager.java:217)
at coldfusion.orm.hibernate.HibernateSessionManager.flushAllCurrentSessions(HibernateSessionManager.java:271)
at coldfusion.orm.hibernate.HibernatePersistenceManager.beforeTransactionBegin(HibernatePersistenceManager.java:927)
at coldfusion.orm.hibernate.HibernatePersistenceManager$HibernateEventListener.beforeTransactionBegin(HibernatePersistenceManager.java:1075)
at coldfusion.tagext.sql.TransactionTag.fireBeforeTransactionBegin(TransactionTag.java:721)
at coldfusion.tagext.sql.TransactionTag.doStartTag(TransactionTag.java:220)
at cfBaseORMService2ecfc567877790$func$TRANSACTIONED.runFunction(C:\ColdFusion9\wwwroot\ColdBox\system\orm\hibernate\BaseORMService.cfc:1170)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2582)
at cfBaseORMService2ecfc567877790$funcSAVE.runFunction(C:\ColdFusion9\wwwroot\ColdBox\system\orm\hibernate\BaseORMService.cfc:719)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:491)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:337)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2360)
at cfProfiles2ecfc1499356787$funcPROVA.runFunction(C:\ColdFusion9\wwwroot\SeoDashboard\handlers\Profiles.cfc:208)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:517)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:496)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:355)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2301)
at coldfusion.tagext.lang.InvokeTag.doEndTag(InvokeTag.java:389)
at coldfusion.runtime.CfJspPage._emptyTcfTag(CfJspPage.java:2723)
at cfController2ecfc1944470730$funcINVOKER.runFunction(C:\ColdFusion9\wwwroot\ColdBox\system\web\Controller.cfc:699)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2582)
at cfController2ecfc1944470730$funcRUNEVENT.runFunction(C:\ColdFusion9\wwwroot\ColdBox\system\web\Controller.cfc:584)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.filter.SilentFilter.invoke(SilentFilter.java:47)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:517)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:496)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:355)
at coldfusion.runtime.CfJspPage._invoke(CfJspPage.java:2301)
at cfColdbox2ecfc1074052954$funcPROCESSCOLDBOXREQUEST.runFunction(C:\ColdFusion9\wwwroot\ColdBox\system\Coldbox.cfc:236)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
at coldfusion.runtime.CfJspPage._invokeUDF(CfJspPage.java:2582)
at cfApplication2ecfc943471585$funcONREQUESTSTART.runFunction(C:\ColdFusion9\wwwroot\SeoDashboard\Application.cfc:58)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:472)
at coldfusion.runtime.UDFMethod$ReturnTypeFilter.invoke(UDFMethod.java:405)
at coldfusion.runtime.UDFMethod$ArgumentCollectionFilter.invoke(UDFMethod.java:368)
at coldfusion.filter.FunctionAccessFilter.invoke(FunctionAccessFilter.java:55)
at coldfusion.runtime.UDFMethod.runFilterChain(UDFMethod.java:321)
at coldfusion.runtime.UDFMethod.invoke(UDFMethod.java:220)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:491)
at coldfusion.runtime.TemplateProxy.invoke(TemplateProxy.java:337)
at coldfusion.runtime.AppEventInvoker.invoke(AppEventInvoker.java:88)
at coldfusion.runtime.AppEventInvoker.onRequestStart(AppEventInvoker.java:258)
at coldfusion.filter.ApplicationFilter.invoke(ApplicationFilter.java:349)
at coldfusion.filter.RequestMonitorFilter.invoke(RequestMonitorFilter.java:48)
at coldfusion.filter.MonitoringFilter.invoke(MonitoringFilter.java:40)
at coldfusion.filter.PathFilter.invoke(PathFilter.java:94)
at coldfusion.filter.LicenseFilter.invoke(LicenseFilter.java:27)
at coldfusion.filter.ExceptionFilter.invoke(ExceptionFilter.java:70)
at coldfusion.filter.ClientScopePersistenceFilter.invoke(ClientScopePersistenceFilter.java:28)
at coldfusion.filter.BrowserFilter.invoke(BrowserFilter.java:38)
at coldfusion.filter.NoCacheFilter.invoke(NoCacheFilter.java:46)
at coldfusion.filter.GlobalsFilter.invoke(GlobalsFilter.java:38)
at coldfusion.filter.DatasourceFilter.invoke(DatasourceFilter.java:22)
at coldfusion.filter.CachingFilter.invoke(CachingFilter.java:62)
at coldfusion.CfmServlet.service(CfmServlet.java:200)
at coldfusion.bootstrap.BootstrapServlet.service(BootstrapServlet.java:89)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:86)
at coldfusion.monitor.event.MonitoringServletFilter.doFilter(MonitoringServletFilter.java:42)
at coldfusion.bootstrap.BootstrapFilter.doFilter(BootstrapFilter.java:46)
at jrun.servlet.FilterChain.doFilter(FilterChain.java:94)
at jrun.servlet.FilterChain.service(FilterChain.java:101)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:106)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invoke(JRunRequestDispatcher.java:286)
at jrun.servlet.ServletEngineService.dispatch(ServletEngineService.java:543)
at jrun.servlet.http.WebService.invokeRunnable(WebService.java:172)
at jrunx.scheduler.ThreadPool$DownstreamMetrics.invokeRunnable(ThreadPool.java:320)
at jrunx.scheduler.ThreadPool$ThreadThrottle.invokeRunnable(ThreadPool.java:428)
at jrunx.scheduler.ThreadPool$UpstreamMetrics.invokeRunnable(ThreadPool.java:266)
at jrunx.scheduler.WorkerThread.run(WorkerThread.java:66)
I think I should insert
profilesService.save©;
before
p.setGAprofiles©;
Think this is the problem…
That error message means that you have not flushed the object back to the database, and have then tried to reference it again.
You might wish to take a gander at how persistent objects work with the database, especially when it comes to flushing and retrieving records before they are persisted.
I have solved!
I should first save the new Gaprofiles and then save the email entity…