0

ZK + hibernate: OneToMany and ManyToOne didn't work

asked 2009-04-24 08:26:07 +0800

TLHP gravatar image TLHP
111 1 3

updated 2009-04-24 09:35:14 +0800

Hi, I must show this question in hibarnate's forum, as the site is in maintenance for now, I put it here :)

Here is my code

Server.java

@Entity
@Table(name = "server")
public class Server
{
   ....
  @OneToMany(mappedBy="server", fetch=FetchType.EAGER)
  @Column(name="id")
  protected Set<Site> sites;
  ....
  public void addSite(Site site)
  {
    site.setServer(this);
    this.getSites().add(site);
  }
  
  public void removeSite(Site site)
  {
    this.getSites().remove(site);
  } 

Site.java

@Entity
@Table(name = "site")
public class Site
{
  @ManyToOne
  @JoinColumn(name="server_id")
  protected Server server;
}

For the test, we have 2 sites and 1 server in database

Site site1 = siteDAO.find(1);
Site site2 = siteDAO.find(2);

Server server1 = serverDAO.find(1);

server1.addSite(site1);
server1.addSite(site2);

serverDAO.merge(server1);   

But here, the merge didn't work, it didn't update database :(
Soneone have an idea, plzz help me

Thanks

P/S: all the configuration of Hibernate were done by Zeta Form Builder

delete flag offensive retag edit

2 Replies

Sort by ยป oldest newest

answered 2009-04-24 08:54:55 +0800

robertpic71 gravatar image robertpic71
1275 1

updated 2009-04-24 13:55:47 +0800

You need to cascade "Updateaction" like this:

@OneToMany(cascade = {CascadeType.PERSIST,
						  CascadeType.MERGE,
						  CascadeType.REMOVE },
				mappedBy = "planung"

However, map OneToMany (without eager) can produce overhead, i.e. list all servers load all sites!
I use this only for small database files. For larger files i map only @xxxtoOne and update with the DAO/Servicelayer from the detail.

Of course, sometimes a mapped detail could be very nice. i.e. if the user can free edit the details (like here (click anonym). The user could change details directly in the grid, but i only need a DAO/ServiceLayer.merge(head). Since i use (live) databinding, i do not need any extra action. Hibernate updates only changed rows -i love that.

/Robert

link publish delete flag offensive edit

answered 2009-04-24 09:55:56 +0800

TLHP gravatar image TLHP
111 1 3

One more time, thanks Robert, it's really useful your knowledge

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2009-04-24 08:26:07 +0800

Seen: 284 times

Last updated: Apr 24 '09

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More