gpt4 book ai didi

hibernate - 何时在 hibernate 中使用 property-ref 属性

转载 作者:行者123 更新时间:2023-12-03 07:02:53 25 4
gpt4 key购买 nike

我已经完成了示例程序。我使用了 property-ref 属性。即使我不使用它仍然可以工作。请让我知道property-ref属性在这个程序中的实际用法是什么。

 <hibernate-mapping package="com.hibernate.onetone">
<class name="Book" table="BOOK">
<id name="id" column="BOOK_ID">
<generator class="native" />
</id>
<property name="title" type="string" column="TITLE" />
<property name="description" type="string" column="DESCRIPTION" />
<property name="publishedDate" type="date" column="PUBLISHED" />

<many-to-one name="author" class="com.hibernate.onetone.Author"
column="author_id" unique="true" not-null="true"
cascade="all" />
</class>
</hibernate-mapping>

<hibernate-mapping package="com.hibernate.onetone">
<class name="Author" table="AUTHOR">
<id name="id" column="AUTHOR_ID">
<generator class="native"/>
</id>
<property name="name" column="NAME" />
<property name="email" column="EMAIL" />

<!-- <property name="serialNumber" column="SerialNumber" /> -->
<one-to-one name="book" class="Book" property-ref="author"
constrained="true" cascade="all"/>
</class>
</hibernate-mapping>

public class Book {
private long id;
private String title;
private String description;
private Date publishedDate;
private Author author;
}
public class Author {
private long id;
private String name;
private String email;
private Book book; {
}

最佳答案

property-ref 属性用于建立双向关联并指定关联的哪一方是关系的所有者。在生成的查询中,仅当关系的所有者更新而不是另一方更新时,才会导致外键更新。

如文档中所示

property-ref (optional): the name of a property of the associated class that is joined to this foreign key. If not specified, the primary key of the associated class is used.

另请查看下面的链接以获取更多信息

http://www.allappforum.com/hibernate/hibernate_o_r_mapping_one_to_one_element.htm

http://www.allappforum.com/hibernate/hibernate_o_r_mapping_many_to_one_element.htm

如果您还可以为您提供映射和执行代码,以便我可以使解释更清楚,那就太好了:)

关于hibernate - 何时在 hibernate 中使用 property-ref 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38039357/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com