Hi All,
I have a problem with a parent-children relationship.
I try to add an Event element on the many side but this isn't saved on db.
this is my code:
Event evento=new Event();
...
//Load the account from DB
Account account = accountManager.getAccount(1);
account.addEvent(evento);
//saveAccount() method execute the saveOrUpdate() using Spring HibernateDaoSupport
accountManager.saveAccount(account);
Hibernate version: 3.2.3.ga
Mapping documents:
@Entity
@Table(name="ev_accounts")
public class Account implements Serializable {
/* Private Fields */
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
...
@org.hibernate.annotations.CollectionOfElements()
@JoinTable(name="ev_authorities",joinColumns= @JoinColumn(name="account_id"))
@Column(name= "authority",nullable=false)
private Set<String> authorities = new HashSet<String>();
@OneToMany(mappedBy="account",cascade={CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE})
private Set<Event> events = new HashSet<Event>();
...
public void addEvent(Event event){
event.setAccount(this);
events.add(event);
}
...
}
@Entity
@Table(name="ev_events")
public class Event implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
...
@Embedded
private EventLocation eventLocation;
@ManyToOne(targetEntity=it.urlandus.events.app.domain.Account.class)
@JoinColumn(name="account_id",nullable=false,updatable=false,insertable=false)
private Account account;
...
}
Debug level Hibernate log excerpt:
I use Spring wiht a GenericHibernateDao based on HibernateDaoSupport in order to manage objects.
Full stack trace of any exception that occurs:
when I call the saveOrUpdate() method on Account object I get no exception but the Event instance are not saved and in my log I see that only the update on account is made.
[org.springframework.orm.hibernate3.SessionFactoryUtils] - Opening Hibernate Session
2008-01-30 19:05:37,710 DEBUG [org.hibernate.impl.SessionImpl] - opened session at timestamp: 12017163377
2008-01-30 19:05:37,717 DEBUG [org.springframework.orm.hibernate3.HibernateTemplate] - Eagerly flushing Hibernate session
2008-01-30 19:05:37,717 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - processing flush-time cascades
2008-01-30 19:05:37,717 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - dirty checking collections
2008-01-30 19:05:37,717 DEBUG [org.hibernate.engine.CollectionEntry] - Collection dirty: [it.urlandus.events.app.domain.Account.events#1]
2008-01-30 19:05:37,723 DEBUG [org.hibernate.engine.Collections] - Collection found: [it.urlandus.events.app.domain.Account.authorities#1], was: [it.urlandus.events.app.domain.Account.authorities#1] (uninitialized)
2008-01-30 19:05:37,723 DEBUG [org.hibernate.engine.Collections] - Collection found: [it.urlandus.events.app.domain.Account.events#1], was: [it.urlandus.events.app.domain.Account.events#1] (initialized)
2008-01-30 19:05:37,726 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 insertions, 1 updates, 0 deletions to 1 objects
2008-01-30 19:05:37,726 DEBUG [org.hibernate.event.def.AbstractFlushingEventListener] - Flushed: 0 (re)creations, 1 updates, 0 removals to 2 collections
2008-01-30 19:05:37,726 DEBUG [org.hibernate.pretty.Printer] - listing entities:
2008-01-30 19:05:37,726 DEBUG [org.hibernate.pretty.Printer] - it.urlandus.events.app.domain.Account{password=f5888d0bb58d611107e11f7cbc41c97a, lastname=Balcon, events=[it.urlandus.events.app.domain.Event#0], enabled=Y, firstname=Marco, username=marco, url=urllll, email=aa@aa.aa, authorities=<uninitialized>, id=1}
2008-01-30 19:05:37,732 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2008-01-30 19:05:37,732 DEBUG [org.hibernate.jdbc.ConnectionManager] - opening JDBC connection
2008-01-30 19:05:37,733 DEBUG [org.hibernate.SQL] -
update
ev_accounts
set
email=?,
enabled=?,
firstname=?,
lastname=?,
password=?,
url=?,
username=?
where
id=?
2008-01-30 19:05:37,734 DEBUG [org.hibernate.jdbc.AbstractBatcher] - Executing batch size: 1
2008-01-30 19:05:37,735 DEBUG [org.hibernate.jdbc.AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2008-01-30 19:05:37,736 DEBUG [org.springframework.orm.hibernate3.SessionFactoryUtils] - Closing Hibernate Session
2008-01-30 19:05:37,736 DEBUG [org.hibernate.jdbc.ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2008-01-30 19:05:37,736 DEBUG [org.hibernate.jdbc.ConnectionManager] - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources
Name and version of the database you are using:
mysql 5
many thanks for your help!
|