java:recursiverelationsinhibernate
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionLast revision | |||
— | java:recursiverelationsinhibernate [2013/06/13 11:20] – rlunaro | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Recursive Entities in Hibernate ====== | ||
+ | |||
+ | Tables can be related with themselves: the foreing key of the table points to the key of the table. | ||
+ | |||
+ | This bizarre thing is very useful when you want to represent data which is hierarchically dependent in the real world: an organization (with many departments and sub-departments depending one of each other), human organizations (a boss with his employees) or more complex structures (a network of offices who depend on local branches, and those branches depend on the general headquarters). | ||
+ | |||
+ | In the language of the databases, this could be represented as this: | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | And the table can be created with this command: | ||
+ | |||
+ | <code sql> | ||
+ | create table hg_struct | ||
+ | ( | ||
+ | id_struct | ||
+ | some_data | ||
+ | id_struct_parent | ||
+ | ); | ||
+ | |||
+ | </ | ||
+ | |||
+ | Ok, I've didn't documented the foreing key reference to the same table. I don't care. | ||
+ | |||
+ | **In Hibernate, | ||
+ | |||
+ | I've set both configurations ('' | ||
+ | |||
+ | Here is my example: | ||
+ | |||
+ | <code java> | ||
+ | |||
+ | @Entity | ||
+ | @Table( name = " | ||
+ | public class Struct implements Serializable | ||
+ | { | ||
+ | private static final long serialVersionUID = 2L; | ||
+ | |||
+ | private int idStruct; | ||
+ | private String someData; | ||
+ | private Struct parent; | ||
+ | private Set< | ||
+ | |||
+ | @Id | ||
+ | @GeneratedValue | ||
+ | @Column( name=" | ||
+ | public int getIdStruct() | ||
+ | { | ||
+ | return idStruct; | ||
+ | } | ||
+ | public void setIdStruct(int idStruct) | ||
+ | { | ||
+ | this.idStruct = idStruct; | ||
+ | } | ||
+ | |||
+ | @Column( name=" | ||
+ | public String getSomeData() | ||
+ | { | ||
+ | return someData; | ||
+ | } | ||
+ | public void setSomeData(String someData) | ||
+ | { | ||
+ | this.someData = someData; | ||
+ | } | ||
+ | |||
+ | @ManyToOne(fetch = FetchType.LAZY) | ||
+ | @JoinColumn( name = " | ||
+ | public Struct getParent() | ||
+ | { | ||
+ | return this.parent; | ||
+ | } | ||
+ | public void setParent( Struct parent ) | ||
+ | { | ||
+ | this.parent = parent; | ||
+ | } | ||
+ | |||
+ | @OneToMany( fetch = FetchType.LAZY, | ||
+ | public Set< | ||
+ | { | ||
+ | return this.children; | ||
+ | } | ||
+ | public void setChildren( Set< | ||
+ | { | ||
+ | this.children = children; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | By the way, you can find a detailed documentation of the one-to-many relationship in hibernate here: | ||
+ | |||
+ | http:// | ||
+ | |||
+ | |||
+ | |||
+ | |||
java/recursiverelationsinhibernate.txt · Last modified: 2022/12/02 22:02 by 127.0.0.1