-->
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: Select einer one-to-many Beziehung
PostPosted: Sat Jan 08, 2011 10:45 am 
Newbie

Joined: Sat Jan 08, 2011 10:33 am
Posts: 1
Hallo,
ich fange gerade an, mich in Hibernate einzuarbeiten und versuche mich gerade an einer bidirektionalen many-to-one Beziehung.

Die Klassen sehen so aus:

Basisklasse
Code:
@Entity
@Table(name="ADMIN_OBJECT")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class DBObject{
    @Column(length = 255, nullable = false)
    private String name;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    @Column(length=1024)
    private String description;
    @Column(nullable = false)
    @Version
    private Long version;
   
   
    public DBObject(){
    }
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public  Long getId(){
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    public Long getVersion() {
        return version;
    }
    public void setVersion(Long version) {
        this.version = version;
    } 
}



Code:
@Entity
@Table(name="ADMIN_USER")
public class User extends DBObject{
    @ManyToOne
    @JoinColumn(name="groupID", nullable = false)
    private Group group;
   
    public User(){
        super();
    }


    public void setGroup(Group group) {
        this.group = group;
    }




    public Group getGroup() {
        return group;
    }   
   
}



Code:
@Entity
@Table(name="ADMIN_GROUP")
public class Group extends DBObject{
    @OneToMany(mappedBy="group")
    private List<User> userList;
   
   
    public Group(){
        super();
    }
        public List<User> getUserList() {
        return userList;
    }


    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
   
}


Speichern/ Selektieren
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
       
        session.save(org);
        session.flush();
       
        Group g = new Group();
        g.setName("Group 1");
        session.save(g);
       
       
       
        User user = new User();
        user.setGroup(g);
        user.setName("User1");
        session.save(user);
       
       
        User user2 = new User();
        user2.setGroup(g);
        user.setName("User2");       
        session.save(user2);
       

        session.flush();
        List<Group> groupList = session.createQuery("from Group as group").list();
        for(Group group: groupList){
            System.out.println(group);
        }



Das generierte Schema sieht so aus, wie ichs wollte, auch das speichern klappt.
Selektiere ich dann einen User, wird auch automatisch das zugehörige Group Objekt erzeugt.
Aber selektiere ich nun wie im Beispiel eine Gruppe, bleibt die UserListe der Gruppe null.
Hat jemand eine Idee, wieso?

*gruß*
Sebastian


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.