Hibernate Initial SessionFactory creation failed - java

I'm trying to use hibernate to fill my jsf selectonemenu in ApplicationBean (in Liferay). The problem is that I got Initial SessionFactory creation failed problem. Before putting my functions in the applicationbean I was setting them in sessionbean and I got no error.
For now the full error
Initial SessionFactory creation failed.
java.lang.ClassCastException: org.hibernate.type.StringType cannot be cast to org.hibernate.type.VersionType

You have very likely a VARCHAR column called VERSION somewhere and Hibernate's reverse engineering tool generates it as:
<version name="version" type="string">
<column name="VERSION" length="20" />
</version>
instead of:
<property name="version" type="string">
<column name="VERSION" length="20" />
</property>
The former is wrong. First, I think that this is not what you want. Second, a string is not allowed for a version field as mentioned in the chapter 5.1.9. Version (optional):
Version numbers can be of Hibernate type long, integer, short, timestamp or calendar.
This problem has been somehow reported in HHH-3002 (actually, it should be assigned to Hibernate Tools, not Hibernate Core) and I see two ways to solve it. Either
fix the mapping manually
rename the column to something else.

The property on one of your domain classes that you've mapped as the class's version is of type string. This is not a valid type for a version. What to change it to will depend on how you are implementing versioning in your underlying database.

Related

LONG as primary key in Hibernate mapping to MySQL

I am trying to implement persistence of some Java objects via Hibernate mapping to a MySQL table. When I commit I get a message saying 'Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1'.
My hypothesis is that the problem is caused from having a long-field in my Java POJO that I want to use as my primary key in the MySQL table. Since I was not able to use datatype LONG as my primary key in MySQL table (ERROR 1170: BLOB/TEXT column 'id' used in key specification without a key length) I concluded from some googling and this post that BIGINT would be the suitable mapping for long. However it is not updating.
My test POJO Personis very simple. It has 3 fields: id (long), firstname (String), lastname (String) with setters and getters, etc.
I do the hibernate mapping in xml (person.hbm.xml) that essentially looks like (minus headings):
<hibernate-mapping>
<class name="hibernatetest.Person" table="hibernatetest">
<id name="id" type="long" column="id" >
<generator class="native"/>
</id>
<property name="firstname">
<column name="firstname" />
</property>
<property name="lastname">
<column name="lastname"/>
</property>
</class>
</hibernate-mapping>
My actual java code snippet that is supposed to save or update the record is simple:
Transaction tr = session.beginTransaction();
Person person = new Person(1,"John","Doe");
session.saveOrUpdate(person);
tr.commit();
And here's that thing, this all works just fine if I change the type of id to an int (Integer) in the Person object and in the MySQL table. However, I do not have that option for the actual objects that I want to persist so the question is; what am I doing wrong or what should I do to get it to work? Thanks.
ADDING Stacktrace:
Hibernate: update hibernatetest set firstname=?, lastname=? where id=?
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73)
at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:57)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3006)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2908)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3237)
at org.hibernate.action.internal.EntityUpdateAction.execute(EntityUpdateAction.java:113)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:265)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:187)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1082)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:317)
at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:175)
at com.hibernate.test.TestMain.main(TestMain.java:38)
nested transactions not supported
UPDATE:
OK, I have finally worked it out. I changed the hibernate generator class from 'native' to 'assigned' and now it works as expected. So now the hibernate mapping looks like:
<hibernate-mapping>
<class name="hibernatetest.Person" table="hibernatetest">
<id name="id" type="long" column="id" >
<generator class="assigned"/>
</id>
<property name="firstname">
<column name="firstname" />
</property>
<property name="lastname">
<column name="lastname"/>
</property>
</class>
</hibernate-mapping>
Must admit I did not know the meaning of that parameter (copied from somewhere) and had no idea it could cause this much headache. Found this explanation which was quite useful.
Apparently I do not have enough credentials to answer my own questions so I guess that it will remain open or if someone provides an empty answer, I will accept it. Thanks.
When you use the saveOrUpdate() method hibernate fires the insert query if the id of the object is null and update if it is any other value. I can see the code,
Person person = new Person(1,"John","Doe"); setting the id to 1 and calling the saveOrUpdate() method. I am assuming there are no entries for the id 1 and hence the error is thrown.
To make it work, you need to make the below changes.
Change the Type of id in person to Long from long(The wrapper class so that it can support null).
Write the constructor new Person("John","Doe"); and save that object.
It is not a good Idea to keep the <generator class="assigned"/> for the transactional data. Instead you should be sticking to the native as you were trying first.
I feel this is a cleaner way to solve your initial problem, even though you have found an alternate solution.

org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update

In my module that I'm working on, I got this error, which is said caused by org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
and java.sql.BatchUpdateException (the full stack trace is in here : click here).
From what I've read from other posts, this is surely caused by violation of primary key. However I couldn't even replicate the problem so that I can at least trace the real bug and solve the problem. Every time I inserted an identical entry with the one which already in the database, it will simply merge with each other and no error. However, I was getting many of this error in the deployment, so I'm not quite sure what's going on in the deployment server (I'm just a student developer, so I'm still kinda 'noob' in this).
I would appreciate it even if anyone can point me to the direction. Thanks. (Inform me if there's something need to be added)
Here is the snippet of hibernate mapping for the module (hope this would help) :
<hibernate-mapping package="edu.umd.cattlab.schema.cattXML.extensions.VaTraffic"
default-lazy="false" default-cascade="all, delete-orphan" schema="vatraffic">
<typedef class="edu.umd.cattlab.schema.hibernate.util.XMLGregorianCalendarType" name="XMLCal"/>
<class name="VaTrafficAssociatedEvent" table="associated_event">
<id name="associatedEventId" column="associated_event_id">
<generator class="sequence">
<param name="sequence">ritis.associated_event_id_seq</param>
</generator>
</id>
<property name="secondaryEventId" column="secondary_event_id" not-null="true" />
<property name="unassociatedTimestamp" type="XMLCal" column="unassociated" />
<property name="autoRelated" column="auto_related" not-null="true" />
<many-to-one name="relationshipType" column="relationship_type" not-null="true" cascade="none" />
</class>
This is the part of java code that utilizes the above mapping: click here
You can have more constraints than just a primary key constraint. Could it be you have a foreign key constraint that you are violating? Or maybe a multiple column unique constraint. Could you please include the DDL for the table you are updating?
In reviewing the logs, it is a violation of the pk constraint. Specifically ERROR: duplicate key violates unique constraint "associated_event_pk"
Trying to determine why this is happening may be a deep dive, but for starters how are you generating values for this field? In your ddl it shows as a "nextval" field but your log appears to indicate there is an explicit value. Where is this value coming from? Why are you not letting postgre set the value itself?

Hibernate type used to map java.lang.Double

I am using "double" in Hibernate .hbm.xml files, and when I generate classes with Hibernate Tool I get class property which has primitive type double. I want this property to be java wrapper type Double. How can I achieve this? If I change this manually in my classes, is it going to give any issues to hibernate?
Thanks
I am using "double" in Hibernate .hbm.xml files, and when I generate classes with Hibernate Tool I get class property which has primitive type double. I want this property to be java wrapper type Double. How can I achieve this?
Is the property nullable in the hbm.xml mapping? If it is, you should get a Double.
If you can't change the mapping, you could override the default behavior:
override the column specifically in reveng.xml
use a type-mapping and not-null set to false to handle it for all columns
It only issue you could have is if your property is null amd the column is declared as not null. So I would advise you to always initialize it.
If you can add below statement under type-mapping in your hibernate.reveng.xml ,then you pojos will be generated with double type as java.lang.Double.
<sql-type jdbc-type="DOUBLE" not-null="false" hibernate-type="double" />
Or
if you can add not-null="false" in your hbm files,that also address your issue.
<property name="salary" type="double">
<column name="SALARY" not-null="false" />
</property>

Hibernate Reference column in table to indicate how to unmarshall an attribute in a different column in the same table

I have an entity that I want to persist through Hibernate (3.2)
The EntityBean has a column that indicates how another value of the entity bean should be unmarshalled:
<class name="ServiceAttributeValue" table="service_attribute_value">
<cache usage="nonstrict-read-write"/>
<id name="id" column="id" type="int-long">
<generator class="native"/>
</id>
<property name="serviceAttribute" type="service-attribute" column="service_attribute" not-null="true" />
<!-- order is important here -->
<property name="value" type="attribute-value" not-null="true">
<column name="service_attribute" />
<column name="id_value"/>
<column name="enum_value"/>
<column name="string_value"/>
<column name="int_value"/>
<column name="boolean_value"/>
<column name="double_value"/>
</property>
</class>
The "service_attribute" column indicates which of the columns for the "value" property to look at when it unmarshalls the value and, more importantly, exactly what Type the value should be, for example the class of the Enum if the enum_value is to be read, or the type of Bean if the the id_value is to be read.
The value property uses a custom CompositeUserType to do the unmarshalling and within this I wish to reference the service_attribute column (although not write to it), however when I try to do this I get the following error:
org.hibernate.MappingException: Repeated column in mapping for entity: com.precurse.apps.rank.model.service.ServiceAttributeValue column: service_attribute (should be mapped with insert="false" update="false")
However within the definition of the composite property these xml attributes are not defined (only within a normal property).
Does anyone know of a way of overcoming this, or if there is a better solution to this propblem.
If you need any more information please let me know,
Cheers
Simon
I had a similar problem and changing the case of one column solved the problem. Could give a try!
e.g., one column could be service_attribute other Service_Attribute.
You can try this. Instead of mapping both values as property on the same table, map one of the property using join to itself and keep the other property as the way it is. This case you will be able to access the same property in both places. Just remember to name the property as different name.
<join table="service_attribute_value">
<key column = "id" />
<property name="serviceAttribute" type="service-attribute" column="service_attribute" not-null="true" />
</join>
<!-- order is important here -->
<property name="value" type="attribute-value" not-null="true">
<column name="service_attribute" />
<column name="id_value"/>
<column name="enum_value"/>
<column name="string_value"/>
<column name="int_value"/>
<column name="boolean_value"/>
<column name="double_value"/>
</property>
based on your description, it seems like what you want to do is creating different subclasses based on the service_attribute. Instead of trying to achieve repeated column mapping which is not allow in hibernate, you can take a look hibernate inheritance mapping.
I Think I found a solution albeit not a very elegant one.
in the
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException {
method of the CompositeUserType the "owner" argument passed to the method contains the id of the object who's service_attribute I want to access.
Annoyingly the actual serviceAttribute of the owner is not accessable or has not been set at this stage (I played around with the ordering of the elements in the hbm.xml config, in case this was an ordering thing, but unfortunatly still no joy), so I can't simply access it.
Anyway the id of the owner object is set, so I then used the session argument to run a HQL query based on the id to access the serviceAttribute which I then used to correctly unmarshall the value property.
The drawback of this solution is that it requires a HQL query as an overhead to the unmarshalling process, although its within the same session, its still not optimal.
If anyone has any ideas for a better solution I'd be very grateful.
Cheers

Hibernate Exception help: TransientObjectException

I am getting the following exception when I try to update an object:
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: ......
Can anyone help???
The object that I am trying to update has the 'lazy' attribute set to false in the mapping file. Seems like hibernate is expecting me to save child objects before it flushes the update???
EDIT (ADDED):
<hibernate-mapping>
<class name="utils.message.Message" table="messages">
<id name="id" column="message_id">
<generator class="native" />
</id>
<property name="message_text" column="message_text" />
<property name="message_file" column="message_file" />
<property name="is_active" column="is_active" type="boolean"/>
<property name="is_global" column="is_global" type="boolean"/>
<property name="start" column="start" type="java.util.Date"/>
<property name="end" column="end" type="java.util.Date"/>
<property name="last_updated" column="last_updated" type="java.util.Date"/>
<many-to-one name="last_updated_by" class="utils.user.User" column="last_updated_by" lazy="false"/>
<many-to-one name="healthDepartment" class="utils.healthdepartment.HealthDepartment" column="health_department_id" lazy="false"/>
</class>
</hibernate-mapping>
TransientObjectException occurs when you save an object which references another object that is transient (meaning it has the "default" identifier value, frequently null) and then flush the Session. This commonly happens when you are creating an entire graph of new objects but haven't explicitly saved all of them. There are two ways to work around this:
As you suggest, you could use cascading of saves to other associated objects. However, cascading wasn't really intended as a workaround for TOE but rather as a convenience for saving a group of related objects that are frequently manipulated together. If you detach your objects without its full set of associated data and then save it with cascading enabled, you could inadvertently delete data you don't want to lose.
Ensure that all transient objects in your graph are explicitly saved as part of your unit of work. This is really just a case of understanding how your application will be creating an object graph and what entities are transient and which might be persistent or detached.
I would recommend reading this entire chapter from the Hibernate docs to understand fully the terminology of transient, persistent and detached:
http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/objectstate.html
dude use property "cascade = true" in mapping. all will well...
V-Boy
With a many-to-one relationship, it would not be appropriate for Hibernate to cascade persistence operations, since the "one" is conceptually an entity shared between many others. This kind of relationship isn't a "child object", as you put it.
You can override this behaviour by explicitly setting the cascade attribute on the relation, or you can manually persist the other end of the many-to-one relations.
App is in a Spring environment. Fix: to run update from within Hibernate environment.

Categories

Resources