From: <leg...@at...> - 2003-10-03 11:06:46
|
The following comment has been added to this issue: Author: tekno Created: Fri, 3 Oct 2003 6:06 AM Body: The main method i use is public static void main(String[] args) throws Exception { Properties props = new Properties(); props.setProperty("hibernate.connection.driver_class", "org.postgresql.Driver"); props.setProperty("hibernate.connection.url", "jdbc:postgresql://lab-dev/LABDEV2"); props.setProperty("hibernate.connection.username", "postgres"); props.setProperty("hibernate.show_sql", "true"); Session session = null; Customer customer = null; SessionFactory factory = null; try { // build factory Configuration cfg = new Configuration().addClass(Customer.class).addClass(Employee.class).setProperties(props); factory = cfg.buildSessionFactory(); System.out.println("Starting first session");// first session gets customer instance session = factory.openSession(); customer = (Customer) session.load(Customer.class, new Long(1275)); System.out.println("Loaded customer " + customer.getId()); session.flush(); session.connection().commit(); session.close(); // update the title field System.out.println("Updating field"); customer.setTitle("MR"); System.out.println("Starting second session");// second session persists update session = factory.openSession(); session.update(customer); session.flush(); session.connection().commit(); session.close(); System.out.println("Finished"); factory.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (session != null) session.close(); if (factory != null) factory.close(); System.exit(0); } } The Customer class is public class Customer { private long id; private String title; private Employee created; public Customer() { } public void setId(long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public long getId() { return id; } public Employee getCreated() { return created; } public void setCreated(Employee employee) { created = employee; } } with mapping <hibernate-mapping> <class name="Customer" table="customers"> <jcs-cache usage="read-write"/> <id name="id" unsaved-value="0"> <generator class="assigned"/> </id> <property name="title"/> <many-to-one name="created" class="Employee" column="created_by"/> </class> </hibernate-mapping> and the employee class is public class Employee { private long id; private String name; public Employee() { } public long getId() { return id; } public String getName() { return name; } public void setId(long id) { this.id = id; } public void setName(String name) { this.name = name; } } with mapping <hibernate-mapping> <class name="Employee" table="staff"> <id name="id"> <generator class="assigned"/> </id> <property name="name"/> </class> </hibernate-mapping> the ccf file i am using jcs.default=DC jcs.default.cacheattributes=org.apache.jcs.engine.CompositeCacheAttributes jcs.default.cacheattributes.MaxObjects=2000 jcs.default.cacheattributes.MemoryCacheName=org.apache.jcs.engine.memory.lru.LRUMemoryCache jcs.default.elementattributes=org.apache.jcs.engine.ElementAttributes jcs.default.elementattributes.IsEternal=false jcs.default.elementattributes.MaxLifeSeconds=24000 jcs.default.elementattributes.IdleTime=180000 jcs.default.elementattributes.IsSpool=false jcs.default.elementattributes.IsRemote=false jcs.default.elementattributes.IsLateral=false The database tables I think are pretty obvious customers with id,tile,created_by and staff with id, name created_by is a foreign key joining customers to staff I initialize the db with insert into staff(id,name) values(1,'test'); insert into customers(id,title,created_by) values(1,'test',1); what else do you need? --------------------------------------------------------------------- View the issue: http://opensource.atlassian.com/projects/hibernate/secure/ViewIssue.jspa?key=HB-377 Here is an overview of the issue: --------------------------------------------------------------------- Key: HB-377 Summary: Hibernate 2.1 appears to break jcs caching Type: Bug Status: Unassigned Priority: Major Project: Hibernate2 Components: core Versions: 2.1 Assignee: Reporter: tekno Created: Thu, 2 Oct 2003 4:34 AM Updated: Thu, 2 Oct 2003 4:34 AM Environment: Development Windows 2000 SP3, JSDK 4.1.2 Deployment Solaris 9 Database Postgresql 7.3 Description: I am getting an AssertionFailure - cannot cache a reference to an object with a null id when I try to do an update. Here is the sample code - NB: This has been working fine for me in hibernate versions 2.0 - 2.03 I have the following two mappings <class name="Customer" table="customers" polymorphism="explicit"> <cache usage="read-write"/> <id name="id" unsaved-value="0"> <generator class="sequence"> <param name="sequence">customers_id_seq</param> </generator> </id> <property name="postcode"/> <many-to-one name="csOwner" class="Employee" column="cs_owner" /> </class> and <class name="Employee" table="staff"> <id name="id"> <generator class="sequence"> <param name="sequence">staff_id_seq</param> </generator> </id> <property name="name"/> </class> the following main() throws the exception Session session = null; Customer customer = null; SessionFactory factory = null; try { // build factory factory = new Configuration().configure().buildSessionFactory(); System.out.println("Starting first session"); // first session gets customer instance session = factory.openSession(); customer = (Customer) session.load(Customer.class, new Long(1275)); System.out.println("Loaded customer " + customer.getId() + " has owner " + customer.getCsOwner().getId()); session.flush(); session.connection().commit(); session.close(); System.out.println("Updating address field"); // update a field customer.setPostcode("TESTING"); System.out.println("Starting second session"); // second session persists update session = factory.openSession(); session.update(customer); session.flush(); session.connection().commit(); session.close(); System.out.println("Finished"); factory.close(); } catch (Exception ex){ ex.printStackTrace(); } finally { if(session!=null)session.close(); if(factory!=null)factory.close(); System.exit(0); } If i remove the cache entry in the customer mapping everything is OK, but other wise i get net.sf.hibernate.AssertionFailure: cannot cache a reference to an object with a null id at net.sf.hibernate.type.EntityType.disassemble(EntityType.java:89) at net.sf.hibernate.impl.CacheEntry.disassemble(CacheEntry.java:36) at net.sf.hibernate.impl.CacheEntry.<init>(CacheEntry.java:27) at net.sf.hibernate.impl.ScheduledUpdate.execute(ScheduledUpdate.java:42) at net.sf.hibernate.impl.SessionImpl.executeAll(SessionImpl.java:2278) at net.sf.hibernate.impl.SessionImpl.execute(SessionImpl.java:2235) at net.sf.hibernate.impl.SessionImpl.flush(SessionImpl.java:2178) at com.lab.model.HibBug.main(HibBug.java:46) --------------------------------------------------------------------- JIRA INFORMATION: This message is automatically generated by JIRA. If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa If you want more information on JIRA, or have a bug to report see: http://www.atlassian.com/software/jira |