-->
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: Problem with list mapping
PostPosted: Mon Sep 15, 2008 6:00 am 
Newbie

Joined: Wed Sep 10, 2008 10:42 am
Posts: 2
I am having problems with a list with one of my projects, and it is really starting to annoy me. I have tried searching the forums, but have come accross nothing.

Any help will be really appreciated.

The context is that I have a user object, which I want to record timestamped events against. I have it working so that it will retrieve items from the database, but when I add a new event and saveOrUpdate() on the user class the list does not get saved.

I have run a debug on it and can see that the list that is being initialised from the db is a Hibernate PersistentList. I get a class cast exception if I try to cast it to an ArrayList. If I add an item to the list, it does appear to be added, it just wont save.

I have been bashing my head against my monitor for days, I am sure I am missing something simple.

Cheers,

Simon

JRE: jdk1.6.0_06
Server: Tomcat 5.5.26
Hibernate version: 3.2.5
JDBC driver: mysql-connector-java-5.1.5
Framework: Struts 1.2.9

User object:
Code:
public class User {
    private long id;
    private String username;
    private Long version;
    private List<Event> events = new ArrayList();

    ....

    public void setEvents(List<Event> events) {
        this.events = events;
    }
    public List getEvents() {
        return this.events;
    }
    public void addEvent(Event event) {
        log.info("Adding event: "+event.toString());
        this.events.add(event);
    }
}

Event object
Code:
public class Event {
    private Date time;
    private String description;

    public Event(String description) {
        this.setTime(new Date());
        this.setDescription(description);
    }
   
    public void setTime(Date time) {
        this.time = time;
    }
    public Date getTime() {
        return this.time;
    }
   
    public void setDescription(String description) {
        this.description = description;
    }
    public String getDescription() {
        return this.description;
    }

    @Override
    public String toString() {
        return "[" + this.time.toString() + "] " + this.description;
    }
}

Mapping document
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class name="User" table="user">
    <id column="userid" name="id" type="long">
      <generator class="native"/>
    </id>
    <version column="version" name="version" type="long"/>
   
    <!-- STANDARD PROPERTIES -->
    <property name="username"/>
   
    <!-- DO THE EVENTS -->
    <list cascade="all-delete-orphan" inverse="true" name="events" table="events">
      <key column="event_id" not-null="true"/>
      <index column="list_position"/>
      <composite-element class="Event">
        <property name="description"/>
        <property name="time" type="java.util.Date" />
      </composite-element>
    </list>
   
  </class>
</hibernate-mapping>

This is from my logs
Code:
11-09 16:53:52 DEBUG [LoadContexts.java:183] creating collection wrapper:[User.events#3]
...
11-09 16:53:53 INFO  [    User.java:127] Adding event: [Thu Sep 11 16:53:53 BST 2008] User identified themselves
11-09 16:53:53 INFO  [ UserDAO.java:44] Saving user
11-09 16:53:53 DEBUG [ThreadLocalSessionContext.java:300] allowing proxied method [saveOrUpdate] to proceed to real session
11-09 16:53:53 DEBUG [AbstractSaveEventListener.java:488] persistent instance of: User
11-09 16:53:53 DEBUG [DefaultSaveOrUpdateEventListener.java:99] ignoring persistent instance
11-09 16:53:53 DEBUG [DefaultSaveOrUpdateEventListener.java:136] object already associated with session: [User#3]
11-09 16:53:53 INFO  [HibernateSessionFilter.java:53] Committing the transaction
11-09 16:53:53 DEBUG [ThreadLocalSessionContext.java:300] allowing proxied method [getTransaction] to proceed to real session
11-09 16:53:53 DEBUG [ThreadLocalSessionContext.java:300] allowing proxied method [getTransaction] to proceed to real session
11-09 16:53:53 DEBUG [JDBCTransaction.java:103] commit
11-09 16:53:53 DEBUG [SessionImpl.java:337] automatically flushing session
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:58] flushing session
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:111] processing flush-time cascades
11-09 16:53:53 DEBUG [ Cascade.java:115] processing cascade ACTION_SAVE_UPDATE for: User
...
11-09 16:53:53 DEBUG [ Cascade.java:291] cascade ACTION_SAVE_UPDATE for collection: User.events
11-09 16:53:53 DEBUG [ Cascade.java:306] done cascade ACTION_SAVE_UPDATE for collection: User.events
...
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:154] dirty checking collections
11-09 16:53:53 DEBUG [CollectionEntry.java:177] Collection dirty: [User.events#3]
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:171] Flushing entities and processing referenced collections
11-09 16:53:53 DEBUG [DefaultFlushEntityEventListener.java:236] Updating entity: [User#3]
11-09 16:53:53 DEBUG [Versioning.java:110] Incrementing: 6 to 7
...
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:210] Processing unreferenced collections
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:224] Scheduling collection removes/(re)creates/updates
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:85] Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:91] Flushed: 0 (re)creations, 1 updates, 0 removals to 2 collections
11-09 16:53:53 DEBUG [ Printer.java:83] listing entities:
11-09 16:53:53 DEBUG [ Printer.java:90] User{id=3, username=sy50, events=<uninitialized>, version=6}
11-09 16:53:53 DEBUG [AbstractFlushingEventListener.java:290] executing flush
11-09 16:53:53 DEBUG [ConnectionManager.java:469] registering flush begin
11-09 16:53:53 DEBUG [AbstractEntityPersister.java:2339] Updating entity: [User#3]
11-09 16:53:53 DEBUG [AbstractEntityPersister.java:2341] Existing version: 6 -> New version: 7



Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 15, 2008 1:55 pm 
Regular
Regular

Joined: Mon Apr 19, 2004 6:54 pm
Posts: 79
have you tried to remove the inverse attribute?
And you need to implement hashCode() and equals() in the Event class.

Christophe


Top
 Profile  
 
 Post subject: Got it working
PostPosted: Wed Sep 17, 2008 7:46 am 
Newbie

Joined: Wed Sep 10, 2008 10:42 am
Posts: 2
I carried on playing and decided to try moving away from the list idea (as I could use the date to order the data).

I settled on an ordered set, which does the trick.

This has fixed the problem.

Thanks for your suggestions though.


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.