-->
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.  [ 1 post ] 
Author Message
 Post subject: Many to Many PropertyAccessException
PostPosted: Thu Feb 02, 2012 4:21 pm 
Newbie

Joined: Thu Feb 02, 2012 4:07 pm
Posts: 2
Hallo!

Ich arbeite erst seit kurzem mit Hibernate und stoße leider schon an meine Grenzen. Ich bin dabei eine Webapplication in Java zu bauen, die in etwa wie citeulike aufgebaut sein sollen. Um die Daten dahinter verwalten zu können, möchte ich gerne Hibernate verwenden. Ein kleiner Datenbankausschnitt macht mir Probleme - Ich habe Usergruppen, in denen User vorhanden sind und der sogenannte Interessensgebiete zugeordnet werden können.

Ich habe mich dazu entschieden, in der Tabelle Usergroup eine ManyToMany Beziehung zu Interessensgebieten (später Fields genannt) bereitzustellen, da ein Feld zu mehreren Usergruppen gehören kann, und eine Usergruppe mehrere Interessensgebiete haben kann.

Ich verwende für mein Mapping Annotationen und hatte mir dieses nachfolgende Konstrukt aus einem Tutorial kopiert. In einer Funktion versuche ich nun das Quers (from Usersgroup) abzusetzen. Hierbei bekomme ich aber einen Stackoverflow - ich gehe davon aus, dass ich irgendwo in der Mapping Konfiguration einen rekursiven Aufruf habe, da die Methode setFields in usergruppe zig tausendfach aufgerufen wird. Ich wäre für Tipps wirklich sehr, sehr dankbar!!

Usergruppe:

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
     */

}


Aufruf:

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";
        }
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.