-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Generated properties-lastModified property is not generated
PostPosted: Mon May 19, 2008 7:02 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
In my POJO, I have a property:
Code:
private Date lastModified


I want this property to be updated as and when user updates the table.

I have followed the notes given in the hibernate docs:
5.6. Generated Properties http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-generated

and also the section Generated and default property values from Hibernate book (page# 182. Chapter 4).

Here is the mapping for the column. My database is Oracle 9i.

Code:
<property name="lastModified"
column="LAST_MODIFIED"
update="false"
insert="false"
generated="always"/>


When I save the entity, A SQL insert statement and a SELECT statement is found in the logs. But the column LAST_MODIFIED is never updated. It has 'null' value and returns the same.


Here what I found from Hibernate JIRA site:

http://opensource.atlassian.com/project ... e/HHH-1471
http://opensource.atlassian.com/project ... e/HHH-2511

Are you able to get generated values for properties like 'lastModified' in your application? Please let me know the fix for it.

Hibernate version: 3.2.6 GA


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 6:56 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
I think you are misinterpreting what is meant by "generated properties" (as I did after the first reading). This does not mean that Hibernate generates the value for these properties (at insert or update time). It means, the database will do this - by executing a trigger which YOU must define.

Carlo
----------------------------
please credit me if this helped you.


Top
 Profile  
 
 Post subject:
PostPosted: Sun May 25, 2008 2:46 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Carlo is correct. You're going to need to explicitly initialize those properties in your code, as such:

Code:
  Session session=HibernateUtil.beginTransaction();
  User user = new User();
  user.setLoginName("mj");user.setPassword("abc123");
  user.setEncryptedPassword("zab012");

  user.setEmailAddress("mj@scjapxw.com");
  user.setLastAccessTime(new java.util.Date());
  user.setRegistrationDate(
            new java.util.GregorianCalendar());
  user.setVerified(Boolean.FALSE);

  session.save(user);
  HibernateUtil.commitTransaction();


The Java code snippet is taken from this Hibernate3 example tutorial on column mapping, for more information:

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=06hibernatetableandcolumnmappingwithjpa

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 27, 2008 6:31 pm 
Senior
Senior

Joined: Fri Jun 01, 2007 12:41 pm
Posts: 121
First, thanks for answering my question.

I don't agree with your idea that generated properties should be managed by application.

Let's read what is mentioned in Hibernate Docs for generated properties:
Quote:
5.6. Generated Properties
Generated properties are properties which have their values generated by the database. Typically, Hibernate applications needed to refresh objects which contain any properties for which the database was generating values. Marking properties as generated, however, lets the application delegate this responsibility to Hibernate. Essentially, whenever Hibernate issues an SQL INSERT or UPDATE for an entity which has defined generated properties, it immediately issues a select afterwards to retrieve the generated values.

Properties marked as generated must additionally be non-insertable and non-updateable.

http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-generated


So here are my questions:

1. If we have to set the property value (e.g. lastModifiedTime with the method
Code:
setLastModifiedTime(Date date)
), what is the use of hibernate feature 'generated properties'.

2. The doc says that: "lets the application delegate this responsibility to Hibernate". This means, Hibernate should assign the value to the property marked as generated before the SQL is issued. Why this is not happening?

3. I have seen that hibernate is issuing a SELECT statement after INSERT/UPDATE for the entity with generated property. But some how it is not assigning any value to the property marked as generated.

4. Inside the mapping for generated property, we have
Code:
insert="false", update="false"
, in that case what is the use of setting the value for property, i.e. lastModifiedTime when it is not part of INSERT/UPDATE statement.

5. Finally, I think hibernate should manage setting the value for 'generated' properties - from Hibernate side or by creating and attaching a trigger (or what ever suitable SQL event for that dialect). So it is not responsibility of the application to assign values for generated properties.

Thanks
Shyam


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.