abstractembeddedcomponents.cid

来源:互联网 发布:淘宝全球购官网 编辑:程序博客网 时间:2024/05/30 02:26

proxy的作用

但是proxy是怎麽回事?它是做什莫用的?困惑我很久了 :(<
比如说User和Group是many-to-one的关系,User中有一个类型为Group的属性group,
若你想要载入一个User时,它的Group属性group要延迟载入,

A 那你就必须为Group类定义proxy,这个proxy一般可以定义为两种
  1 Group类本身
  2 一个被Group类实现的,且包含了Group类的所有业务方法的接口,这种做法可以克服第一种做法的一些缺点,但会比较麻烦,因为要定义一个接口嘛。

B 映射文件中这个<many-to-one>的outer-join要定义为
  1 "false"(如果你在hibernate.properties或hibernate.cfg.xml文件中定义了hibernate.use_outer_join true)
  2 "auto"(默认的)或"false"(hibernate.use_outer_join false),(我没有试过当hibernate.use_outer_join false时,<many-to-one>的outer-join定义为"true",会不会延迟载入和还是会用outer join一并取出关联对象(在这里就是group对象),你可以试试) 

个人认为hibernate.use_outer_join true是全局的设置 ,而<many-to-one>的outer-join是局部的,局部优先级应该大于全局优先级,没有测试

以后要测试。

hibernate中proxy是用cglib生成的动态代理,你可以看看相应的原码。再仔细看看文档加上实践,应该不难弄懂的:)

 

discriminator 用法:

本质是面向对象的思想,一个表对应多个类,基类定义了discriminator属性说明名称,类型,长度

后面的基类

 <subclass name="MyInterfaceImpl" extends="MyInterface" discriminator-value="1" proxy="MyInterface">
    </subclass>

 

可能经常遇到这样的情况:

       在数据库表中会有这样的一个字段用来区别记录的属性,如:在客户表中有一个字段表示客户级别,当这个记录为A时是一级客户,为B时是二级客户。在用 hiberante做OR表示时类可能是这样的:

       public class Customer{

          private String flag;   //表示客户的级别

          ...

       }   

       然后,在程序中手动控制flag的值,但是这样当每个级的客户有不同的属性时Customer类将包含所有级别的属性,这样不是很好。

       hibernate提供一个Discriminator映射的方法,就是把一个表映射成不同的类,有不同的属性。

       public class Customer{

          //包含所有级别的公共属性

          ...

        }

 

       public class CustomerA extends Customer{

       //只包括一级客户的特有属性

        }

       public class CustomerB extends Customer{

       //只包含二级客户特有的属性

        }

这样更符合面向对象的原则,然后在hbm.xml中这样写:

<id name="id" type="int">

    ...

</id>

<discriminator column="flag" type="string" />

<!-- 公共属性的映射 -->

<subclass name="CustomerA" discriminator-value="A">

<!-- 一级客户特有属性的映射 -->

</subclass>

<subclass name="CustomerB" discriminator-value="B">

<!-- 二级客户特有属性的映射 -->

</subclass>

这样就可以单独的用CustomerA,CustomerB这样的实例了,做数据库修改时就不用关心flag字段的值了,会自动的加A或B。

如果是使用hibernate Annotation而不是xml来描述映谢关系,代码如下:

@Entity

@Table(name = "customer")

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)

@DiscriminatorColumn(name = "flag", discriminatorType = DiscriminatorType.STRING)

public class Customer{

}

@Entity

@DiscriminatorValue(value = "A")

public class CustomerA extends Customer{

}

@Entity

@DiscriminatorValue(value = "B")

public class CustomerB extends Customer{

}

这样就可以了。

 

 

<class name="MyInterface" table="MY_INTF" proxy="MyInterface">
        <composite-id>
            <key-property name="key1" type="string"/>
            <key-property name="key2" type="string"/>
        </composite-id>   //复合组件
        <discriminator column="TYPE" type="string" length="10"/>   这个在class中不用说明
        <property name="name" type="string"/>
    </class>

    <subclass name="MyInterfaceImpl" extends="MyInterface" discriminator-value="1" proxy="MyInterface">  基类中也没有Type属性
    </subclass>

这让我想到了以前做项目遇到的问题。机场与航路点 应该在一张表中 ,后来却分成2张表,导致程序写起来费劲 而且不安全。如果知道这种方法就好了。

MyInterfaceImpl myInterface = new MyInterfaceImpl();
  myInterface.setKey1( "key1" );
  myInterface.setKey2( "key2" );
  myInterface.setName( "test" );

  Session s = openSession();
  Transaction t = s.beginTransaction();
  s.save( myInterface );
  s.flush();
  List<MyInterfaceImpl> m =s.createQuery( "from MyInterface" ).list();

  s.delete( myInterface );
  t.commit();
  s.close();

当我监测数据库时   s.flush(); 执行完毕 数据库没有数据 ,但是确实已经提交了sql语句

  List<MyInterfaceImpl> m =s.createQuery( "from MyInterface" ).list(); 执行完这句,也返回了数据库的值

这说明flush仅仅提交sql语句 ,在一个session中能被查询到。

commit 会首先执行flush方法,然后提交事务。

这里也会知道复合组件的使用 。

复合组件 要使用public interface MyInterface extends Serializable ,实现Serializable接口,否则报错。

org.hibernate.MappingException: composite-id class must implement Serializable: org.hibernate.test.abstractembeddedcomponents.cid.MyInterface

 

 

 


 

原创粉丝点击