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?
|