Hi!
I'm currently working on a web application which should look like citeulike, and there I've got some problems, I'm not able to solve out.
In my database scheme I've got two tables USERGROUP and FIELDS. One Usergroup contains a lot of user and can have a lot of fields (fields of interest). I tried to solve this relationship with a many to many annotation, which resulted in a stackoverflow error which leads to a propertyaccessexception.
I'm not sure whether my manytomany mapping is right - I think there could be a problem with recursive calls - because the setter "setFields" is calles approximately a thousand times ; ).
I would appreciate any help!! Thanks in advance!!
Usegroup:
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package at.fh.hagenberg.mbi.swo5.shop.domain;
import java.io.Serializable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToOne;
import org.hibernate.annotations.Table;
/**
*
* @author Daniela
*/
@Entity
@Table(appliesTo="UsersGroup")
public class UsersGroup implements Serializable {
private Integer id;
private String name;
private Set<Field> fields = new HashSet<Field>(0);
private LiteratureList list;
/**
* @return the id
*/
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the fields
*/
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "Usergroup_Fields", joinColumns = { @JoinColumn(name = "id") }, inverseJoinColumns = { @JoinColumn(name = "field_name") })
public Set<Field> getFields() {
return fields;
}
/**
* @param fields the fields to set
*/
public void setFields(Set<Field> fields) {
this.setFields(fields);
}
/**
* @return the list
*/
@OneToOne(cascade = CascadeType.ALL)
public LiteratureList getList() {
return list;
}
/**
* @param list the list to set
*/
public void setList(LiteratureList list) {
this.list = list;
}
/**
* @return the name
*/
@Column(name="name")
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
}
Fields
Code:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package at.fh.hagenberg.mbi.swo5.shop.domain;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import org.hibernate.annotations.Table;
/**
*
* @author Daniela
*/
@Entity
@Table(appliesTo="Field")
public class Field implements Serializable {
private String field_name;
private Set<UsersGroup> groups = new HashSet<UsersGroup>(0);
/**
* @return the field_name
*/
@Id
@Column(name="field_name", nullable=false)
public String getField_name() {
return field_name;
}
/**
* @param field_name the field_name to set
*/
public void setField_name(String field_name) {
this.field_name = field_name;
}
/**
* @return the groups
*/
@ManyToMany(fetch = FetchType.LAZY, mappedBy = "fields")
public Set<UsersGroup> getGroups() {
return groups;
}
/**
* @param groups the groups to set
*/
public void setGroups(Set<UsersGroup> groups) {
this.groups = groups;
}
/**
* @return the word
*/
}
Call:
Code:
public String fetchAllGroups() {
try {
Session session = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory().getCurrentSession();
session.beginTransaction();
Query q = session.createQuery("from UsersGroup");
List groupList = q.list();
for (Object o : groupList) {
UsersGroup group = (UsersGroup) o;
getGroups().add(group);
}
return "ok";
} catch (Exception ex) {
Logger.getLogger(UserGroupHelper.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
return "error";
}
}
EDIT: Problem solved - i'm now using field annotations instead of getter Annotations...