Java Persistence With Hibernate第四章

注:以下内容是阅读Java persistence with Hibernate后,自己理解到的,只涉及Hibernate  core 的内容,不涉及JPA等注释的内容


4.1 Understanding entities and value types理解实体类型和值类型)<o:p></o:p>

<st1:chsdate w:st="on" year="1899" month="12" day="30" islunardate="False" isrocdate="False">4.1.1</st1:chsdate> Fine-grained domain models细粒度领域模型)<o:p></o:p>

<o:p> </o:p>

       比如我们的领域中有User这个模型,包含的字段有地址(城市、街名、邮编),在数据库中user表内包含了城市、街名、邮编等字段;但在建模时,最好是将地址列为单独的一个类,使之与user想关联,这样能更好的使用聚合,复用代码。<o:p></o:p>

<o:p> </o:p>

<st1:chsdate w:st="on" year="1899" month="12" day="30" islunardate="False" isrocdate="False">4.1.2</st1:chsdate> Defining the concept(定义概念)<o:p></o:p>

<o:p> </o:p>

       实体类型<o:p></o:p>

作为一个独立的实体存在,其生命周期独立于其他实体,可以被其他实体共享.如在数据库中有其对应的标识(identity),对应一条记录,该实体(记录)可以在数据库中共享(标识作为外键使用)。<o:p></o:p>

<o:p> </o:p>

       值类型<o:p></o:p>

              不能作为一个独立的实体存在,只能依附于其他实体,作为其他实体的一部分,其生命周期依赖其所依附的实体(宿主),不能被多个实体共享。如在User Address类型,是依赖于其所附属的user,在数据库中表现为user表的字段。<o:p></o:p>

<o:p> </o:p>

<st1:chsdate w:st="on" year="1899" month="12" day="30" islunardate="False" isrocdate="False">4.1.3</st1:chsdate>Identifying entities and value types<o:p></o:p>

1、判断该模型是否需要标识<o:p></o:p>

2、判断该模型是否只能被一个实体拥有<o:p></o:p>

3、判断是否能被多个实体引用<o:p></o:p>

<o:p> </o:p>

4.2 Mapping entities with identity<o:p></o:p>

<st1:chsdate w:st="on" year="1899" month="12" day="30" islunardate="False" isrocdate="False">4.2.1</st1:chsdate>understand java identify and equality<o:p></o:p>

       1objects are identical if they occupy the same location in the JVM,  using ‘==’ notion to denote that.<o:p></o:p>

       2Object are equality if they have the same value, using the method equals(Object o) to check if they are equality.<o:p></o:p>

<o:p> </o:p>

<st1:chsdate w:st="on" year="1899" month="12" day="30" islunardate="False" isrocdate="False">4.2.2</st1:chsdate> Handing database identity<o:p></o:p>

       1In POJOs, you should declare a identity property, for example :long id, but you should take attention to that whether you need a method setId();<o:p></o:p>

       2mapping the identity property to database primary key column, you should take attention to the id generator, native? Assigned? Or increment.<o:p></o:p>

<st1:chsdate w:st="on" year="1899" month="12" day="30" islunardate="False" isrocdate="False">4.2.3</st1:chsdate> database primary key<o:p></o:p>

 A primary key must meet :<o:p></o:p>

       1:    Its value is never null<o:p></o:p>

       2:    Each row has a unique value<o:p></o:p>

       3:    the value of a particular row never change<o:p></o:p>

Sometimes an identity property means nothing about the owing object, just to identify it.<o:p></o:p>

4.4 Generated and default property values<o:p></o:p>

4.4.1 basic class mapping

1、如果一个持久类的某些字段不需要自行赋值,而使用数据库产生值,要求该类的对象持久化之前,这些字段为null值,此时获取返回null;持久化(存入数据库)后,这些字段为数据库默认值,此时获取返回默认值。<o:p></o:p>

<o:p> </o:p>

Hibernate实现流程:<o:p></o:p>

       每次insert 或者 update对象时,立即产生一个select查询,并将默认值返回到对象中。<o:p></o:p>

XML文件:<o:p></o:p>

<property name="lastModified"><o:p></o:p></property>

<property name="lastModified"><o:p></o:p></property>

<property name="lastModified"<o:p></o:p>

column="LAST_MODIFIED"<o:p></o:p>

update="false"<o:p></o:p>

insert="false"<o:p></o:p>

generated="always"/><o:p></o:p>

<o:p></o:p>

<o:p> </o:p>

Annotations:<o:p></o:p>

@Column(updatable = false, insertable = false)<o:p></o:p>

@org.hibernate.annotations.Generated(<o:p></o:p>

org.hibernate.annotations.GenerationTime.ALWAYS<o:p></o:p>

)<o:p></o:p>

private Date lastModified;<o:p></o:p>

   注意:此时该类不应该存在更改该属性的方法,如setLastModified方法<o:p></o:p>

2 如果类的某一字段所对应的数据字段有默认值,如,price$1;要求:如果对象的属性为null,则使用默认值;如果不为空,则使用该值.

<class name="Item" table="ITEM"<o:p></o:p>

dynamic-insert="true" dynamic-update="true"><o:p></o:p>

...<o:p></o:p>

<property

    name="initialPrice" type="big_decimal"><o:p></o:p>

column name="INITIAL_PRICE"<o:p></o:p>

default="'1'"<o:p></o:p>

generated="insert"/><o:p></o:p>

</property><o:p></o:p>

...<o:p></o:p>
</class><o:p></o:p>

<o:p></o:p>

<o:p></o:p>

<o:p></o:p>

<o:p> </o:p>

注意:必须指定dynamic-insert="true" dynamic-update="true"  ,否则还是插入null值,而且持久化该对象后,你必须进行flush才能在对象中获取默认值,如:<o:p></o:p>

Item newItem = new Item(...);<o:p></o:p>

session.save(newItem);<o:p></o:p>

newItem.getInitialPrice(); // is null<o:p></o:p>

session.flush(); // Trigger an INSERT<o:p></o:p>

// Hibernate does a SELECT automatically<o:p></o:p>

newItem.getInitialPrice(); // is $1<o:p></o:p>

<o:p> </o:p>

<o:p>4.4.2 Mapping components</o:p>

<o:p>    在之前的Address和User例子中,Address最为值类型,而User最为实体类型,如何将Address映射到User表中的字段的呢?</o:p>

<o:p>    注意:值类型没有标识(identity)</o:p>

<o:p>    public class Address {
        private String street;
        private String zipcode;
        private String city;
        public Address() {}
        public String getStreet() { return street; }
        public void setStreet(String street) { this.street = street; }
        public String getZipcode() { return zipcode; }
        public void setZipcode(String zipcode) {
            this.zipcode = zipcode; }
        public String getCity() { return city; }
        public void setCity(String city) { this.city = city; }
    }
</o:p>

<o:p>    Hibernate使用Component属性:</o:p>

< class name="User" table="USER">
        < id name="id" column="USER_ID" type="long">
            < generator class="native"/>
        < id>
        < property name="loginName" column="LOGIN" type="string"/>

        <
component name="homeAddress" class="Address">
            < property name="street" type="string"
                                column="HOME_STREET" not-null="true"/>
            < property name="city" type="string"
                                column="HOME_CITY" not-null="true"/>
            < property name="zipcode" type="string"
                                column="HOME_ZIPCODE" not-null="true"/>
        < /component>
</class>

<o:p>    我们也可以指定Address对象所处在的实体对象(User):</o:p>

<o:p>    在Address类中添加User user;字段,并在User XML配置文件中的Component属性中添加子属性</o:p>

             < parent name="user"/>

<o:p> </o:p>

<o:p> </o:p>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值