Plu*_*tor 6 java database schema hibernate
我正在尝试为数据库模式构建一个Hibernate层,我基本上无法控制.简化后,有两个表.
表parent有两个重要的列:
parent_id,整数,主键,自动增量parent_code,字符串,唯一键,由某个黑盒子生成(让我们说这是一个UUID,为了理智)表child有两个重要的列:
child_parent_id,整数,主键,自动增量child_parent_code,string,指向父级parent_code值的外键我希望能够调用Parent.getChilds()并获取Child对象的集合.但是设置Hibernate映射文件似乎是不可能的.它使用下面的映射做的是child用parent_id值(而不是parent_code)搜索表.
在Parent.hbm.xml:
<set name="childs" inverse="true" lazy="true" table="child" fetch="select">
<key>
<column name="child_parent_code" not-null="true" />
</key>
<one-to-many class="foo.bar.Child" />
</set>
Run Code Online (Sandbox Code Playgroud)
在Child.hbm.xml:
<many-to-one name="parent" class="foo.bar.Parent" fetch="select">
<column name="child_parent_code" not-null="true" />
</many-to-one>
Run Code Online (Sandbox Code Playgroud)
我花了一个小时仔细阅读我的Java Persistence with Hibernate副本,但我无法弄清楚如何做我想做的事情.可能吗?
Jes*_*sse 12
我会考虑使用hibernate注释.我发现它们更容易使用xml定义.
这是注释格式的代码:
@Entity
@Table(name="parent")
public class Parent
{
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name="child", referencedColumnName = "id")
private Child child;
}
@Entity
@Table(name = "child")
public class Child
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
@Column(name = "code")
public String code;
}
Run Code Online (Sandbox Code Playgroud)
在父级中尝试这样的事情:
<set name="childs" inverse="true" lazy="true" table="child" fetch="select">
<key column="child_parent_code" property-ref="code" />
<one-to-many class="foo.bar.Child" />
</set>
Run Code Online (Sandbox Code Playgroud)
这在孩子身上:
<many-to-one name="parent" class="foo.bar.Parent"
fetch="select" column="child_parent_code" property-ref="code" />
Run Code Online (Sandbox Code Playgroud)
我假设父级中的代码属性称为“代码”。