-->
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.  [ 2 posts ] 
Author Message
 Post subject: saveorUpdate method reinit collection
PostPosted: Thu Mar 25, 2010 6:35 am 
Newbie

Joined: Thu Mar 25, 2010 5:55 am
Posts: 3
Hi,

Im a newbie in hibernate and id like to implement a simple struts2+hibernate crud.
I
have a pb with the saveorUpdate() method. When I try to update the parent with new element in the child Set it reinit the child Set before updating.

I am a little bit lost, could u explain to me what's wrong in my code?

I have two classes with an onetomany association.

the parent is:

@Entity
@Table(name="MATRIX")
public class Matrix {
private Long id;
private String name;
private Columndef cldef = new Columndef();
private Set<Columndef> values = new HashSet<Columndef>(0);

@Id
@GeneratedValue
@Column(name="MATRIX_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String val) {
name = val;
}
@OneToOne(cascade = CascadeType.ALL)
public Columndef getCldef() {
return cldef;
}
public void setCldef(Columndef cl) {
cldef = cl;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "MATRIX_COLUMNDEF", joinColumns = { @JoinColumn(name = "MATRIX_ID") }, inverseJoinColumns = { @JoinColumn(name = "COLUMNDEF_ID") })
public Set<Columndef> getValues() {
return values;
}
public void setValues(Set<Columndef> lst) {
if (lst != null) {
if (lst.size() > 0) values = lst;
}
}
public void addValue(Columndef cl) {
values.add(cl);
}
}

and the child is:

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="MATRIX")
public class Matrix {
private Long id;
private String name;
private Columndef cldef = new Columndef();
private Set<Columndef> values = new HashSet<Columndef>(0);

@Id
@GeneratedValue
@Column(name="MATRIX_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String val) {
name = val;
}
@OneToOne(cascade = CascadeType.ALL)
public Columndef getCldef() {
return cldef;
}
public void setCldef(Columndef cl) {
cldef = cl;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "MATRIX_COLUMNDEF", joinColumns = { @JoinColumn(name = "MATRIX_ID") }, inverseJoinColumns = { @JoinColumn(name = "COLUMNDEF_ID") })
public Set<Columndef> getValues() {
return values;
}
public void setValues(Set<Columndef> lst) {
if (lst != null) {
if (lst.size() > 0) values = lst;
}
}
public void addValue(Columndef cl) {
values.add(cl);
}
}

My action is:

public class MatrixAction extends ActionSupport implements ModelDriven<Matrix> {
/**
*
*/
private static final long serialVersionUID = -2662966220408285700L;

private Matrix cl = new Matrix();
private List<Matrix> clList = new ArrayList<Matrix>();
private MatrixDAO clDAO = new MatrixDAOImpl();
private Set<Columndef> lst = new HashSet<Columndef>();
private List<Columndef> lst2 = new ArrayList<Columndef>();

@Override
public Matrix getModel() {
return cl;
}

/**
* To save or update cl.
* @return String
*/
public String saveOrUpdate()
{
clDAO.saveOrUpdateMatrix(cl);
return SUCCESS;
}

and my DAO is:

public class MatrixDAOImpl implements MatrixDAO {

@SessionTarget
Session session;

@TransactionTarget
Transaction transaction;

@Override
public void saveOrUpdateMatrix(Matrix cl) {
try {
session.saveOrUpdate(cl);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}
If I add a new element to the cl.values Set I can check that the cl.values is correct before the saveOrUpdate() method. But unfortunately it doesn' save the updated cl.values Set in DB.

thks in advance,

bruno


Top
 Profile  
 
 Post subject: Re: saveorUpdate method reinit collection
PostPosted: Thu Mar 25, 2010 6:38 am 
Newbie

Joined: Thu Mar 25, 2010 5:55 am
Posts: 3
I made a mistake in the previous post, the Child is:

@Entity
@Table(name="COLUMNDEF")
public class Columndef {
private Long id;
private String name;
private Set<Stringclass> values = new HashSet<Stringclass>(0);
@Id
@GeneratedValue
@Column(name="COLUMNDEF_ID")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String val) {
name = val;
}
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "COLUMNDEF_STRINGCLASS", joinColumns = { @JoinColumn(name = "COLUMNDEF_ID") }, inverseJoinColumns = { @JoinColumn(name = "STRINGCLASS_ID") })
public Set<Stringclass> getValues() {
return values;
}
public void setValues(Set<Stringclass> lst) {
values = lst;
}
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.