-->
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: Representing a tree structure
PostPosted: Fri Oct 05, 2007 11:12 am 
Newbie

Joined: Thu Oct 04, 2007 5:46 am
Posts: 2
Is it possible to represent the following class in Hibernate in one table. Note the nested type array.

public class AcquirEDModule
{
private String name;
private long id;
private AcquirEDModule[] children;

public AcquirEDModule(){}

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 AcquirEDModule[] getChildren() {
return children;
}

public void setChildren(AcquirEDModule[] children) {
this.children = children;
}
}

Thanks
Matt


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 05, 2007 1:43 pm 
Newbie

Joined: Tue Aug 07, 2007 2:31 am
Posts: 11
Depending on how you expect to walk the tree, it might not be terribly efficient, however it is doable.

One way would be to have each node store it's parent, that way you only need one table. If you were to do this in annotations it would look like:
Code:
public class AcquirEDModule
{
@Column(length = 255)
private String name;

@Id @GeneratedValue(strategy=GenerationType.AUTO)
private long id;

@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY, cascade=CascadeType.ALL)
private AcquirEDModule[] children;

@ManyToOne(optional=false, fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE})
private AcquirEDModule parent;
}


With the table structure of

Code:
table Route (
        id bigint not null,
        name varchar(255),
        parent bigint,
        primary key (id)
}


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.