ORM - Linked Table Error - orm

I'm working on a blog application using ColdBox 3.5 and ColdFusion 10 ORM, and I'm randomly getting following error message:
Error Type: Application : [N/A]
Error Messages: Exception in Hibernate operation.
Either the updated/deleted row does not exist or the session contained stale data.
Root cause :org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Entry#297e1bfa369cf17501369d26ffae00a4]
/model/entry/Entry.cfc:
component
persistent="true"
table="Entry"
output="false"
{
// primary key
property name="entryID" fieldtype="id" ormtype="string" type="string" length="32" generator="uuid";
// properties
property name="title" fieldtype="column" ormtype="string" type="string" length="100" notnull="true";
property name="alias" fieldtype="column" ormtype="string" type="string" length="100" notnull="true";
property name="body" fieldtype="column" ormtype="string" type="string" sqltype="nvarchar(max)" notnull="true";
property name="allowComments" fieldtype="column" ormtype="boolean" type="boolean" sqltype="bit" dbdefault="1" default="true" notnull="true";
property name="released" fieldtype="column" ormtype="boolean" type="boolean" sqltype="bit" dbdefault="1" default="true" notnull="true";
property name="releasedDate" fieldtype="column" ormtype="timestamp" type="date";
property name="addDate" fieldtype="timestamp" ormtype="timestamp" type="date";
// relations
property name="categories" fieldtype="many-to-many" cfc="model.category.Category" linktable="EntryCategory" fkcolumn="entryID" inversejoincolumn="categoryID" singularname="category" cascade="all" lazy="true";
property name="comments" fieldtype="one-to-many" cfc="model.entryComment.EntryComment" fkcolumn="entryID" singularname="comment" cascade="all-delete-orphan";
property name="user" fieldtype="many-to-one" cfc="model.user.User" fkcolumn="userID" notnull="true" cascade="save-update";
property name="views" fieldtype="one-to-many" cfc="model.entryView.EntryView" fkcolumn="entryID" singularname="view" cascade="all-delete-orphan";
// validation
this.constraints = {
"title" = {"required" = true},
"body" = {"required" = true},
"allowComments" = {"required" = true, type="boolean"},
"released" = {"required" = true, type="boolean"},
"categories" = {size=1},
"user" = {type="component"}
};
}
/model/category/Category.cfc:
component
persistent="true"
table="Category"
schema="system"
output="false"
{
// primary key
property name="categoryID" fieldtype="id" ormtype="string" type="string" length="32" generator="uuid";
// properties
property name="name" fieldtype="column" ormtype="string" type="string" length="50" notnull="true";
property name="alias" fieldtype="column" ormtype="string" type="string" length="50" notnull="true";
property name="description" fieldtype="column" ormtype="string" type="string" default="" length="200";
property name="addDate" fieldtype="timestamp" ormtype="timestamp" type="date";
property name="active" fieldtype="column" ormtype="boolean" type="boolean" sqltype="bit" dbdefault="1" default="true" notnull="true";
// relations
property name="entries" fieldtype="many-to-many" cfc="model.entry.Entry" linktable="EntryCategory" fkcolumn="categoryID" inversejoincolumn="entryID" lazy="true" cascade="all" singularname="entry" inverse="true";
// validation
this.constraints = {
"name" = {"required" = true},
"active" = {"required" = true, type="boolean"}
};
}
Here's the code that I'm running:
<cfscript>
entry = entityNew("Entry", {
"title" = "test",
"body" = "test",
"alias" = "test",
"allowComments" = 0,
"released" = 0,
"user" = entityLoadByPK("User", "297e1bfa3697d377013697f53ca10084")
});
// works 1 out of 5 times
entry.setCategories([entityLoadByPK("Category", "297e1bfa36986e69013698c3e54f000d")]);
// works every time
//entry.setCategories(entityLoad("Category", "297e1bfa36986e69013698c3e54f000d"));
//entry.setCategories(entityLoad("Category"));
entitySave(entry);
ormFlush();
</cfscript>
Notice the sections labeled as "works 1 out of 5 times" and "works every time". I don't get what I'm doing wrong. I have other objects similar to these where they use linked table and I'm getting similar error messages. I've reviewed the SQL log. The error appears to occur when it gets ready to insert to EntryCategory table. Any ideas?

Use HQL and use where ID in (?). That works very well.
If the entity already has an array, use ArrayClear() first.
UPDATE:
categories = ormExecuteQuery("from Category where Id IN (:Ids)",
{Ids=listToArray(FORM.categoryIDs)});
entry.setCategories(categories);

Related

Oracle issue: ORA-00972: identifier is too long / NHibernate.Exceptions.GenericADOException: could not execute query

This is the query performed by NHibernate against the Oracle database:
select
compteurra0_.NO_SEQ_CPTE_RAPP_ACCES_INFO_DSQ as NO1_2_,
compteurra0_.TXT_INFO_COMPL as TXT2_2_,
compteurra0_.TYP_CPTE_RAPP_ACCES_DSQ as TYP3_2_,
compteurra0_.VAL_CPTE_RAPP_ACCES_DSQ as VAL4_2_,
compteurra0_.VAL_CPTE_ATNDU as VAL5_2_,
compteurra0_.ID_UTIL_CREAT_OCC as ID6_2_,
compteurra0_.DHC_OCC as DHC7_2_,
compteurra0_.NO_SEQ_RAPP_ACCES_INFO_DSQ as NO8_2_
from
ESO.ESO_V_CPTE_RAPP_ACCES_DSQ compteurra0_
When I perform this query against the database, it returns an Oracle error:
ORA-00972: identifier is too long
I have searched the Internet and found that a bug was reported back in 2005 using NHibernate and Oracle: Oracle issue: ORA-00972: identifier is too long
Has this really been solved?
I have found two other related SO questions stating resolutions using Hibernate in Java.
Can the same be done using NHibernate?
If so, how to make it work?
hibernate oracle identifier is too long ORA-00972
Fluent Nhibernate Oracle Identifier Too Long - Alias Name Issue
CompteurRapportAcces.cs
public class CompteurRapportAcces : AuditableEntity {
public virtual string InformationComplementaire { get; set; }
public virtual RapportAccesInformation Rapport { get; set; }
public virtual TypeCompteur Type { get; set; }
public virtual int Valeur { get; set; }
public virtual int ValeurAttendue { get; set; }
public enum TypeCompteur {
Ordonnance = 1,
Delivrance = 2,
OrdonnanceElectronique = 3,
InscriptionRegistreDesRefus = 4
}
}
CompteurAccesRapport.hbm.xml
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="QueContientMonDSQ.Model" assembly="QueContientMonDSQ">
<class name="CompteurRapportAcces" table="ESO_V_CPTE_RAPP_ACCES_DSQ" schema="ESO">
<id name="Id" column="NO_SEQ_CPTE_RAPP_ACCES_INFO_DSQ" type="Int32" unsaved-value="0">
<generator class="sequence-identity">
<param name="sequence">ESO_NO_SEQ_CPTE_RAPP_ACCES_DSQ</param>
<param name="schema">ESO</param>
</generator>
</id>
<property name="InformationComplementaire" column="TXT_INFO_COMPL" type="String" length="1000" />
<property name="Type" column="TYP_CPTE_RAPP_ACCES_DSQ" type="Int32" />
<property name="Valeur" column="VAL_CPTE_RAPP_ACCES_DSQ" type="Int32" />
<property name="ValeurAttendue" column="VAL_CPTE_ATNDU" type="Int32" />
<property name="Creator" column="ID_UTIL_CREAT_OCC" type="String" length="15" />
<property name="Created" column="DHC_OCC" />
<many-to-one name="Rapport" class="RapportAccesInformation" column="NO_SEQ_RAPP_ACCES_INFO_DSQ" />
</class>
</hibernate-mapping>
hibernate.cfg.xml
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="QueContientMonDSQ">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="format_sql">true</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
<id name="Id" column="NO_SEQ_CPTE_RAPP_ACCES_INFO_DSQ" type="Int32" unsaved-value="0">
<generator class="sequence-identity">
<param name="sequence">ESO_NO_SEQ_CPTE_RAPP_ACCES_DSQ</param>
<param name="schema">ESO</param>
</generator>
</id>
The length of identifier(column name) NO_SEQ_CPTE_RAPP_ACCES_INFO_DSQ must be <= 30.

Cascade ="all" not saving child entities

I think the object model below is saying a Party and PartyName to have a many to one relatioship. An I think the cascade=all i the Party.hbm sshould be having NHib save the child PartyName(s).
But it clearly isn't...
Can someone explain why PartyName isn't being saved with Party and what to do to fix?
Cheers,
Berryl
MAPPING
<class name="Party" table="Parties">
<id name="Id">
<column name="PartyId" />
<generator class="hilo" />
</id>
<discriminator column="Type" not-null="true" type="string" />
<set access="field.camelcase-underscore" cascade="all" inverse="true" name="Names">
<key foreign-key="Party_PartyName_FK">
<column name="PartyNameId" />
</key>
<one-to-many class="Parties.Domain.Names.PartyName, Parties.Domain" />
</set>
<subclass
name="Smack.Core.TestingSupport.NHibernate.TestableDomain.SomeDopeyDomainModel.Student, Smack.Core.TestingSupport"
discriminator-value="Student"
>
<property name="Number" />
<many-to-one
class="Smack.Core.TestingSupport.NHibernate.TestableDomain.SomeDopeyDomainModel.Course, Smack.Core.TestingSupport"
foreign-key="Course_FK"
name="Course">
<column name="CourseId" index="CourseIndex" />
</many-to-one>
</subclass>
<many-to-one access="field.camelcase-underscore" class="Parties.Domain.Party" foreign-key="Party_FK" name="Party">
<column name="PartyId" index="PartyIndex" not-null="true"/>
</many-to-one>
<property name="TheRequiredName" not-null="true" length="50"/>
<property name="EverythingElse" />
<property name="ContextUsed" length="50"/>
<property name="Salutation" length="20"/>
<property name="EffectivePeriod" type="Smack.Core.Data.NHibernate.UserTypes.DateRangeUserType, Smack.Core.Data">
<column name="EffectiveStart"/>
<column name="EffectiveEnd"/>
</property>
Failing Test (and output)
[Test]
public void CanSaveAndLoad_AllProperties()
{
var partyName = NameSeeds.DevName;
partyName.Party = _party;
Assert.That(_party.Names.First(), Is.EqualTo(partyName));
using (var tx = _session.BeginTransaction())
{
_session.Save(_party);
tx.Commit();
}
_session.Clear();
Party foundParty;
using (var tx = _session.BeginTransaction())
{
foundParty = _session.Get<Party>(_party.Id); *** <=== name s/b saved!!
tx.Commit();
}
PartyName foundName = foundParty.Names.First();
//found.Look();
Assert.That(foundName, Is.EqualTo(partyName));
Assert.That(foundName.Party, Is.Not.Null);
Assert.That(foundName.TheRequiredName, Is.EqualTo(partyName.TheRequiredName));
Assert.That(foundName.EverythingElse, Is.EqualTo(partyName.EverythingElse));
Assert.That(foundName.ContextUsed, Is.EqualTo(partyName.ContextUsed));
Assert.That(foundName.Salutation, Is.EqualTo(partyName.Salutation));
Assert.That(foundName.EffectivePeriod, Is.EqualTo(partyName.EffectivePeriod));
}
NHibernate: INSERT INTO Parties (Type, PartyId) VALUES ('Parties.Domain.Party', #p0);#p0 = 32768 [Type: Int32 (0)]
NHibernate: SELECT party0_.PartyId as PartyId2_0_, party0_.Number as Number2_0_, party0_.CourseId as CourseId2_0_, party0_.Type as Type2_0_ FROM Parties party0_ WHERE party0_.PartyId=#p0;#p0 = 32768 [Type: Int32 (0)]
With the mapping of the Names <set> inverse=true, you will have to explicitly call session.Save(partyNameObject) on each member of the collection. If you are looking to have NHibernate automatically save the members of the set when the PartyObject is saved, you need to change the Names <set> inverse attribute to inverse=false. This tells Nhibernate that you want Party to control the relationship between Party and PartyName. You must also remember to add each partyNameObject to the Party.Names collection. Otherwise, they won't be saved when you call Session.Save(partyObject). Keep in mind that having the parent control the relationship may be handy, but if you happen to save the PartyObject without having Loaded the PartyNames collection, NHibernate will update their Party FK to Null. In this scenario with certain Cascade options set on the Names <set>, you might find Nhibernate Deleting them as well.

Nhibernate: On insert new record updating all existing records in table

I'm having a problem with NHibernate. I found this issue in NHibernate Profiler. When I try to insert a new record in the table It's triggering update statement for each record in table.
Like if I have 1000 record in database on insert or update its triggering update statement for each record in database.
Here is my sample
Here is my hbm file and sample code
<property name="ParentCodeId" column="Parent_Code_ID" not-null="false" />
<property name="Code" column="Diagnostic_Code" not-null="false" />
<property name="Descr" column="Description" not-null="true" />
<property name="Level" column="Level" not-null="true" />
<property name="CreatedBy" column="Created_By" not-null="true" />
<property name="CreatedDate" column="Created_Date" not-null="true" />
<property name="ModifiedBy" column="Modified_By" not-null="false" />
<property name="ModifiedDate" column="Modified_Date" not-null="false" />
<property name="IsActive" column="Is_Active" not-null="true" />
var repository = this.DomainObjectRepositoryFactory.Create<DiagnosticCodeRepository, DiagnosticCode>();
repository.BeginTransaction();
diagnosticCode.Id = data.DiagnosticCodeId;
diagnosticCode.ParentCodeId = data.ParentCodeId;
diagnosticCode.Code = data.Code;
diagnosticCode.Descr = data.Descr;
diagnosticCode.IsActive = data.IsActive;
diagnosticCode.Level = data.Level;
// save Diagnostic code
id = repository.Save(diagnosticCode);
repository.Commit();
Any Idea?
Thanks
Imran
Is diagnosticCode a new object?
I don't know if this is your problem, but you might have better luck if you pull the object out of the database, make changes, then save it. You shouldn't ever have to set the id property on your objects by hand.

Nhibernate query issue

Considering the following mapping file where TemporaryAccessOpenCommand and TemporaryAccessCloseCommand both inherit from base class Command
<class name="Command" table="xxx_Commands" lazy="false">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="BeginDate" />
<property name="EndDate" />
<component name="Result" class="CommandResult">
<property name="Status" column="ResultStatus"/>
<property name="Details" column="ResultDetails" />
</component>
<many-to-one name="Requestor" class="xxx.Domain.SessionInfo, xxx.Domain" column="SessionInfoId" lazy="false" />
<joined-subclass name="xxx.TemporaryAccess.Commands.TemporaryAccessCloseCommand, xxx.TemporaryAccess" table="xxx_Commands_TemporaryAccess_Close">
<key column="CommandId"/>
</joined-subclass>
<joined-subclass name="xxx.TemporaryAccess.Commands.TemporaryAccessOpenCommand, xxx.TemporaryAccess" table="xxx_Commands_TemporaryAccess_Open">
<key column="CommandId"/>
<many-to-one name="EndUser" ...... />
<property name="Reason"/>
<property name="AccessRight"/>
<property name="AccessType"/>
<bag name="CloseAccessCommands" cascade="all" lazy="false">
<key column="OpenCommandId"/>
<one-to-many class="xxx.TemporaryAccess.Commands.TemporaryAccessCloseCommand, xxx.TemporaryAccess"/>
</bag>
</joined-subclass>
What would be the nhibernate query to retrieve every OpenAccessCommand having no succeeded CloseAccessCommand ?
I tried this :
public IList<TemporaryAccessOpenCommand> FindOpenCommandsWithoutSucceededCloseCommand()
{
return this.HibernateTemplate.ExecuteFind<TemporaryAccessOpenCommand>(delegate(ISession session)
{
return session.CreateCriteria(typeof(TemporaryAccessOpenCommand))
.CreateCriteria("CloseAccessCommands")
.Add(Expression.Not(Expression.Eq("Result.Status", CommandStatus.Succeeded)))
.List<TemporaryAccessOpenCommand>();
});
}
But it will return an OpenAccessCommand that has tow CloseCommand (one failed and one succeeded) when it should return an empty list.
Thanks for your help (and excuse my poor english)
I managed to get something working this way (I'm open to suggestions if it's not the best way)
public IList<TemporaryAccessOpenCommand> FindOpenCommandsWithoutSucceededCloseCommand()
{
return this.HibernateTemplate.ExecuteFind<TemporaryAccessOpenCommand>(delegate(ISession session)
{
string sql = string.Format(#"
(
SELECT temp_c.OpenCommandId
FROM VSA2_Commands_TemporaryAccess_Close temp_c
INNER JOIN VSA2_Commands c
ON temp_c.CommandId = c.Id
WHERE c.ResultStatus = {0}
) AS OpenCommandId"
, (int)CommandStatus.Succeeded);
var subCriteria = DetachedCriteria.For<TemporaryAccessCloseCommand>();
subCriteria = subCriteria.SetProjection(Projections.SqlProjection(sql, new string[] { "OpenCommandID" }, new IType[] { NHibernateUtil.Int32 }));
return session.CreateCriteria(typeof(TemporaryAccessOpenCommand))
.Add(Expression.Eq("Result.Status", CommandStatus.Succeeded))
.Add(Subqueries.PropertyNotIn("Id", subCriteria))
.List<TemporaryAccessOpenCommand>();
});
}

Hibernate - New colums from a joined table [duplicate]

This question already has answers here:
Hibernate One-To-Many Unidirectional on an existing DB
(4 answers)
Closed 2 years ago.
I have a class User object. I am trying to load this object from the Database using Hibernate.
My SQL statement is:
SELECT
users.uid AS uid,
users.deleted AS deleted,
users.username AS username,
users.passwd AS passwd,
users.disabled AS disabled,
users.lockout AS lockout,
users.expires AS expires,
data_firstname.value AS firstname,
data_lastname.value AS lastname
FROM
ac_users as users
LEFT JOIN
ac_userdef_data as data_firstname
ON
users.uid = data_firstname.parentuid AND
data_firstname.fieldname like 'firstname'
LEFT JOIN
ac_userdef_data as data_lastname
ON
users.uid = data_lastname.parentuid AND
data_lastname.fieldname like 'lastname'
WHERE
users.deleted = 0
My mapping for the User class is:
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<generator class="native"/>
</id>
<property name="deleted" column="deleted" />
<property name="username" column="username" />
<property name="password" column="passwd" />
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="disabled" column="disabled" />
<property name="lockout" column="lockout" />
<property name="expires" column="expires" />
</class>
My problem is that the table ac_users does not have a column 'firstname' or 'lastname'
These columns, only exist in my resultset from the SQL-join statement.
They do also not exist in the ac_userdef_data table. There i have 2 colums: fieldname and value. and 2 rows:
fieldname = 'firstname' with some value in the value column
and another row with
fieldname = 'lastname' with some value in the value column
How do i change my mapping file, so that Hibernate will understand that it needs to load the firstname and lastname column into my firstname and lastname fields on my POJO, while those columns dont actually exist on the referenced ac_users table.?
I now have some working code. The trick was to specify a loading Query for the User class. Hibernate then validates against the return from the Query instead of the Table design.
Class mapping
<class name="com.agetor.commons.security.User" table="ac_users">
<id name="uid" column="uid" type="long" >
<generator class="native"/>
</id>
<property name="deleted" column="deleted" />
<property name="username" column="username" />
<property name="password" column="passwd" />
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="disabled" column="disabled" />
<property name="lockout" column="lockout" />
<property name="expires" column="expires" />
<loader query-ref="GetAllUsers" />
</class>
My Query declaration
<sql-query name="GetAllUsers">
<return alias="user" class="com.agetor.commons.security.User" />
<![CDATA[
SELECT
users.uid AS uid,
users.deleted AS deleted,
users.username AS username,
users.passwd AS passwd,
users.disabled AS disabled,
users.lockout AS lockout,
users.expires AS expires,
data_firstname.value AS firstname,
data_lastname.value AS lastname
FROM
ac_users as users
LEFT JOIN
ac_userdef_data as data_firstname
ON
users.uid = data_firstname.parentuid AND
data_firstname.fieldname like 'firstname'
LEFT JOIN
ac_userdef_data as data_lastname
ON
users.uid = data_lastname.parentuid AND
data_lastname.fieldname like 'lastname'
WHERE
users.deleted = 0
]]>
</sql-query>
I had to use the line <return alias="user" class="com.agetor.commons.security.User" /> so that the returned collection was typed correctly