-->
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.  [ 3 posts ] 
Author Message
 Post subject: Access the value of autoinc field after save()
PostPosted: Mon Sep 03, 2007 8:47 am 
Newbie

Joined: Mon Sep 03, 2007 8:23 am
Posts: 2
I'm using the latest hibernate and spring with MySQL 5.0.45 and am using the autoinc annotation in my model as follows:

/**
* @return the id
*/
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getAddressId() {
return addressId;
}

/**
* @param id the id to set
*/
public void setAddressId(Long id) {
this.addressId = id;
}

AddressDAO is declared as:

public interface AddressDAO extends GenericDao<Address, Long> {
...
}


In my client I can't get the value of new records. They are created ok. I'm using this:

String[] path = {"applicationContext-resources.xml", "applicationContext-dao.xml"};

try {
ApplicationContext ctx = new ClassPathXmlApplicationContext(path);
AddressDAOHibernate addressDAO = (AddressDAOHibernate) ctx.getBean("addressDAO");

Address address = new Address();
address.setName("New Name");

...

addressDAO.save(address);

addressDAO.getHibernateTemplate().flush();
addressDAO.getHibernateTemplate().clear();
address = addressDAO.get(address.getAddressId());

The calls to flush() & clear() are my attempts to force the id back. The last line fails with an invalid key message, and inspecting address.getAddressId() at that point shows it to be null.

Has anyone seen this before please?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 06, 2007 10:03 am 
Newbie

Joined: Mon Sep 03, 2007 8:23 am
Posts: 2
Anyone please?

After more research I found @PostPersist, which sounds like a great hook to use to get the id, so I tried adding a log to my Address method and adding this annotation, but it never gets called.

@PostPersist
private void postPersist() {
String s = LogSession.generateFormattedSessionRef();
log.debug(s + "---- Entry ----");
log.debug(s + "Class " + this);
log.debug(s + "---- Exit ----");
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 06, 2007 11:22 am 
Newbie

Joined: Thu Sep 06, 2007 10:24 am
Posts: 2
Maybe you should use refresh(address) after flush(). It seems to be created for this :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.