??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品第一视频,久久久999精品,欧美日韩视频在线第一区http://www.aygfsteel.com/jackybu/category/1616.html<a ><b><font color=red>共有<script src=http://fastonlineusers.com/online.php?d=jackybu.blogjava.net></script>人在同时阅读此Blog</font></b></a>zh-cnWed, 28 Feb 2007 03:39:20 GMTWed, 28 Feb 2007 03:39:20 GMT60[转]hibernate:one-to-one的学习小l验http://www.aygfsteel.com/jackybu/articles/10501.html?/dc:creator>?/author>Fri, 19 Aug 2005 05:45:00 GMThttp://www.aygfsteel.com/jackybu/articles/10501.htmlhttp://www.aygfsteel.com/jackybu/comments/10501.htmlhttp://www.aygfsteel.com/jackybu/articles/10501.html#Feedback0http://www.aygfsteel.com/jackybu/comments/commentRss/10501.htmlhttp://www.aygfsteel.com/jackybu/services/trackbacks/10501.html one-to-one在hibernate中可以用来作Z张表之间的主键关联,q也是hibernate中主键关联的一U用法,q样在一张表中的IDQ在生成另外一张表的同时回自动插入到相应的ID字段中去Q相应的XML文g讄比较单,举例如下Q?BR>
    <!-- 建立一对一的到Address的映,q个是写在User的XML配置文g中的 -->
    <!-- 相应的User beanQPOQ中也要d属?com.xx.Address  address-->
    <one-to-one name="address" cascade="all" class="com.xx.Address"/>
   
   <!-- cascade的属性设|不再重复了Q可以查看hibernate文档 -->

    <!-- 建立一对一的到User的映,q个是写在Address的XML配置文g中的 -->
    <!-- 相应的Address beanQPOQ中也要d属?com.xx.User user--> -->
    <one-to-one name="user" class="com.xx.User" constrained="true"/>

      Z在Address中用User中的主键ID|我们需要设|Address中的主键生成规则Q如下所C,采用foreign关键?BR>
   <id column="ID" name="id" type="long" unsaved-value="0">
      <generator class="foreign">
        <param name="property">user</param> 
      </generator>
   </id>


      q里需要注意的是property的属性值必M上面到User的映所填写的name属性g?/EM>Q这样就完成了one-to-one的映关pR?BR>
上面的过E都很简单,下面我来说说q里需要注意的地方Q?BR>
  1.   在设|属性ID的时候必L意字D늚长度Q如W者这样用oracle的sequence来生成IDQ其长度?4位之长,则应选择hibernatecdlongQ对应的实体中应选择LongQ这样不会出现溢出的情况?/STRONG>


  2.   在测试的时候必要注意q两张表之间因ؓ已经存在了一对一的关p,所以我们不能只?BR>         user.setAddress(address);
         而忽略了
         address.setUser(user);
         q样在做插入的时候会报出attempted to assign id from null one-to-one property: address的错误,q一点初学者会l常犯,W者也是其中之一?BR>

 3.   如果不写cascade="all"或者写成cascade="none"的话Q即使你写了
         user.setAddress(address);
         address.setUser(user);
       也不会发生Q何事情,只有user会被存储?BR>
以上是一些笔者经历的经验,如果有不对的地方Ƣ迎指正?img src ="http://www.aygfsteel.com/jackybu/aggbug/10501.html" width = "1" height = "1" />

]]>
[转]One-to-one关系映射http://www.aygfsteel.com/jackybu/articles/10497.html?/dc:creator>?/author>Fri, 19 Aug 2005 05:30:00 GMThttp://www.aygfsteel.com/jackybu/articles/10497.htmlhttp://www.aygfsteel.com/jackybu/comments/10497.htmlhttp://www.aygfsteel.com/jackybu/articles/10497.html#Feedback0http://www.aygfsteel.com/jackybu/comments/commentRss/10497.htmlhttp://www.aygfsteel.com/jackybu/services/trackbacks/10497.html对于hibernate?/SPAN>one-to-one关系来说Q大家常常把它忽略,认ؓ它很单,其实q里面有些细节需要注意,?/SPAN>hibernate3中有两种实现one-to-one的方法:W一U就是用many-to-one来代替一对多Q其?/SPAN>one-to-one也就?/SPAN>many-to-one的一U极限方式,若把many-to-oneunique="true"Q则q时候的many-to-one实质上就?/SPAN>one-to-one;q里Z么能够用many-to-one来代?/SPAN>one-to-one呢?最Ҏ的是两个对象必须有一个字D늛兌Q那么你也发?/SPAN>one-to-one中没?/SPAN>column属性,也就是不能够?/SPAN>one-to-one的这U关pȝ成一个字D?/SPAN>/属性,?/SPAN>many-to-one可以Q它?/SPAN>column属性。所以,说了q么多,你应该明白ؓ什么可以用many-to-one来代?/SPAN>one-to-one.

 

对于q种Ҏ要注意几点:

例如Q下面的介绍?/SPAN>use中的两个字段同时影射adressQ只能够实现单向one-to-one,也就是从use?/SPAN>adressQ不能够实现?/SPAN>adress?/SPAN>use.然但下面也介l了可以使用复杂的方法来解决Q但是得不偿失?/SPAN>

 

W二U方法:Z主键兌?/SPAN>one-to-one,q种Ҏ比较直接。也是?/SPAN>adress的主键值和use的一样就可以了,通过

<id name="id" column="ADDRESS_ID">

<generator class="foreign">

<param name="property">user</param>

</generator>

来把adress的主键值和use的主键值相{。其余的讄都是事?/SPAN>

 

对于初学者来说应该注意:单在use?/SPAN>adress中设|?/SPAN>one-to-one是不行的Q需要设|上面的代码Q需要实?SPAN lang=EN-US style="FONT-SIZE: 14pt; mso-font-kerning: 0pt">one-to-one两端的对象主键值相同,q是最主要?/SPAN>。这也是one-to-one?/SPAN>many-to-one的不同之处。应当注意?/SPAN>

                                                                                           fasttalk

                                                                              www.aygfsteel.com/asktalk

 

 

 

Using a foreign key association

The easiest way to represent the association from User to its billingAddress is to use a <many-to-one> mapping with a unique constraint on the foreign key. This may surprise you, since many doesn’t seem to be a good description of either end of a one-to-one association! However, from Hibernate’s point of view, there isn’t much difference between the two kinds of foreign key associations. So, we add a foreign key column named BILLING_ADDRESS_ID to the USER table and map it as follows:

 

<many-to-one name="billingAddress"

class="Address"

column="BILLING_ADDRESS_ID"

cascade="save-update"/>

 

Note that we’ve chosen save-update as the cascade style. This means the Address will become persistent when we create an association from a persistent User. Probably,  cascade="all" makes sense for this association, since deletion of the User should result in deletion of the Address. (Remember that Address now has its own entity lifecycle.)

 

Our database schema still allows duplicate values in the BILLING_ADDRESS_ID column of the USER table, so two users could have a reference to the same address. To make this association truly one-to-one, we add unique="true" to the <many-toone> element, constraining the relational model so that there can be only one user per address:

 

<many-to-one name="billingAddress"

class="Address"

column="BILLING_ADDRESS_ID"

cascade="all"

unique="true"/>

 

This change adds a unique constraint to the BILLING_ADDRESS_ID column in the DDL generated by Hibernate—resulting in the table structure illustrated by figure 6.7.

 

But what if we want this association to be navigable from Address to User in Java?  From chapter 3, you know how to turn it into a bidirectional one-to-many collection—but we’ve decided that each Address has just one User, so this can’t be the right solution. We don’t want a collection of users in the Address class. Instead, we add a property named user (of type User) to the Address class, and map it like so in the mapping of Address:

 

<one-to-one name="user"

class="User"

property-ref="billingAddress"/>

 

This mapping tells Hibernate that the user association in Address is the reverse direction of the billingAddress association in User.

 

In code, we create the association between the two objects as follows:

Address address = new Address();

address.setStreet("646 Toorak Rd");

address.setCity("Toorak");

address.setZipcode("3000");

Transaction tx = session.beginTransaction();

User user = (User) session.get(User.class, userId);

address.setUser(user);

user.setBillingAddress(address);

tx.commit();

image002.gif

To finish the mapping, we have to map the homeAddress property of User. This is easy enough: we add another <many-to-one> element to the User metadata, mapping a new foreign key column, HOME_ADDRESS_ID:

 

<many-to-one name="homeAddress"

class="Address"

column="HOME_ADDRESS_ID"

cascade="save-update"

unique="true"/>

 

The USER table now defines two foreign keys referencing the primary key of the ADDRESS table: HOME_ADDRESS_ID and BILLING_ADDRESS_ID.

 

Unfortunately, we can’t make both the billingAddress and homeAddress associations bidirectional, since we don’t know if a particular address is a billing address or a home address. (We can’t decide which property name—billingAddress or homeAddress—to use for the property-ref attribute in the mapping of the user property.) We could try making Address an abstract class with subclasses HomeAddress and BillingAddress and mapping the associations to the subclasses. This approach would work, but it’s complex and probably not sensible in this case.  Our advice is to avoid defining more than one one-to-one association between any two classes. If you must, leave the associations unidirectional. If you don’t have more than one—if there really is exactly one instance of Address per User—there is an alternative approach to the one we’ve just shown. Instead of defining a foreign key column in the USER table, you can use a primary key association.

 

Using a primary key association

Two tables related by a primary key association share the same primary key values.  The primary key of one table is also a foreign key of the other. The main difficulty with this approach is ensuring that associated instances are assigned the same primary key value when the objects are saved. Before we try to solve this problem, let’s see how we would map the primary key association.

 

For a primary key association, both ends of the association are mapped using the <one-to-one> declaration. This also means that we can no longer map both the billing and home address, only one property. Each row in the USER table has a corresponding row in the ADDRESS table. Two addresses would require an additional table, and this mapping style therefore wouldn’t be adequate. Let’s call this single address property address and map it with the User:

 

<one-to-one name="address"

class="Address"

cascade="save-update"/>

 

Next, here’s the user of Address:

<one-to-one name="user"

class="User"

constrained="true"/>

 

The most interesting thing here is the use of constrained="true". It tells Hibernate that there is a foreign key constraint on the primary key of ADDRESS that refers to the primary key of USER.

 

Now we must ensure that newly saved instances of Address are assigned the same identifier value as their User. We use a special Hibernate identifier-generation strategy called foreign:

 

<class name="Address" table="ADDRESS">

<id name="id" column="ADDRESS_ID">

<generator class="foreign">

<param name="property">user</param>

</generator>

</id>

...

<one-to-one name="user"

class="User"

constrained="true"/>

</class>

 

The <param> named property of the foreign generator allows us to name a one-toone association of the Address class—in this case, the user association. The foreign generator inspects the associated object (the User) and uses its identifier as the identifier of the new Address. Look at the table structure in figure 6.8.  The code to create the object association is unchanged for a primary key association;  it’s the same code we used earlier for the many-to-one mapping style.

image004.gif



]]>
[转]one-to-one的效率问题,用one-to-many来替代?http://www.aygfsteel.com/jackybu/articles/10495.html?/dc:creator>?/author>Fri, 19 Aug 2005 05:26:00 GMThttp://www.aygfsteel.com/jackybu/articles/10495.htmlhttp://www.aygfsteel.com/jackybu/comments/10495.htmlhttp://www.aygfsteel.com/jackybu/articles/10495.html#Feedback0http://www.aygfsteel.com/jackybu/comments/commentRss/10495.htmlhttp://www.aygfsteel.com/jackybu/services/trackbacks/10495.html
         From tableMain

仅仅执行看v来十分简单的一条语句,你所期望的是他紧紧查询T_MAINq张主表Q可是结果确实hibernate通过多个外连接将所有的子表一口气的全部查询出?BR>
         select * from t_main main outer join t_sub1 sub1 on main.id = sub1.id outer join t_sub2 sub2 on main.id = sub2.id...

 如此的效率绝对让你头痛不Ԍ不仅如此Q如果你通过首先获得子表t_sub1的某个主键IDQ然后通过q个主键查询出子表对象,在关联至住表Q同L情况又会发生Q又会生成类似的SQL语句Q这样一来看来对于这个设计应用one-to-one本n是一U错误,是这样吗Q?BR>
         或许有h认ؓ我们在每个one-to-one中加入lazy="true"q个属性会杜绝上述情况的发生,l过W者的证实即便你加入了lazy="true"Q也不会带来M的改变;又或者在hibernate.config中加入fetch depth属性以及在每个兌中设|outer-join="false"Q这些都不会引v本质上的变化Q加入outer-join="false"其实l果只是原有的outer join语句改变成多条sql语句而已Qƈ没发生什么本质变化,反而效率更低了?BR>
         该怎么办呢Q我们先仔细研究一下one-to-one的概念,one to one代表一对一Q在一般的模型中很会遇到one-to-oneq种概念Q因Z十分一对一的概念,好比一个h他只有一个n体和一个头而已Q头和n体是十分好的例子Q因为有w体必定只有一个头Q而且说到了n体必定要说头Q就好像看了某个奛_的n材必定想知道她的长相如何Q?_-Q,所以在q时我们使用one-to-oneQ因U一对一的关pL很强的,而且从对象中取得body必定会取得他所兌的headQ这L情况下用outer-join是十分方便和有效率的Q因为它使用了outer join查询从而避免了两条到数据库的查询语句,而且在这U情况下也只需要在body_hbm.xml中设|一个one-to-one卛_Q所以在q种实是一对一而且在主表中一对一的关联个敎ͼ即主表中one-to-one标签Q十分少的情况下Q用one-to-one是一U很不错的解军_法?BR>
         如果一个主表会对多个子表都q行one-to-one兌呢,像我们一开始遇到的q种情况Q比如你不仅仅只想了解那个你中意的女孩的w材和脸蛋,而且q想知道他的学历Qn世等{一切,在这U情况下Q如果我们都是用多个one-to-one在主表中的话Q那情况正如我们一开始看见的Q是十分可怕的Q该怎么做呢Q不妨考虑一下用one-to-manyQ什么,many?一开始听到manyq个词的时候,我也觉得挺惊讶的q明明是多个一对一的关联ؓ什么要用到many呢?其实manyq没有一定要说是大于一的,你就只在它的many中存在一个关联它有能乃你何呢Q如果用到many的话Q我们就需要改动数据表的设计了Q在每个有关q的子表中加入一列main_id代表主表中该记录的主键子D|只需要这样子改动可以了Q这h带来的效果绝Ҏ值得你这样做的,然后我们按照以往的one-to-many来设计就好了

         在body.hbm.xml加入Q一到head的关联D例,其他的关联按照这L格式d卛_Q?BR>         <set name="head" inverse="true" lazy="true" cascade="all-delete-orphan">
            <key column="ID0000"/>
            <one-to-many class="com.xx.Head"/>
          </set>

         在head.hbm.xml加入
         <many-to-one name="body" column="ID0000" class="com.xx.Body" not-null="true"/>

         行了Q经q上面的改动我们摆׃查询时多个outer-join的困扎ͼ只在需要的时候才对子表进行查询,因ؓ讄了lazy="true"Q所以一切的一切都在我们的预料之中Q我们如果希望获得body的话hibernatel对不会把它的head 也查询出来,节省了查询是所需要的负担Q除非到了我们十分需要head的情冉|会进行关联查询,获得所需要的headl果?BR>
         所以由此看?FONT color=#0000ff>在one-to-oneq种一对一的关pM是很强的情况下,或者是在一张表中存在多个one-to-one的情况下Q用one-to-many来代替one-to-one不失ZU不错的做法Q当然更重要的良好的数据库设计,hibernate毕竟只是末,千万不要本末倒置?BR>

]]>
Hibernate的检索方?/title><link>http://www.aygfsteel.com/jackybu/articles/9687.html</link><dc:creator>?/dc:creator><author>?/author><pubDate>Tue, 09 Aug 2005 13:21:00 GMT</pubDate><guid>http://www.aygfsteel.com/jackybu/articles/9687.html</guid><wfw:comment>http://www.aygfsteel.com/jackybu/comments/9687.html</wfw:comment><comments>http://www.aygfsteel.com/jackybu/articles/9687.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/jackybu/comments/commentRss/9687.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/jackybu/services/trackbacks/9687.html</trackback:ping><description><![CDATA[<!--StartFragment --><FONT style="BACKGROUND-COLOR: #ff0000"> Q:</FONT><span id="wmqeeuq" class=postbody>Hibernate的检索方式有很多 <BR>对象囄D索,load/get索,HQL(find,Query)索,QBC(CriteriaQQBE)索以及本地数据库SQL? <BR>那么Q在实际应用中到底选择哪一U检索好Q它们之间的索有哪些联系和区别呢Q?/SPAN> <BR><BR><!--StartFragment --><FONT style="BACKGROUND-COLOR: #ff0000"> A:(by Robbin)</FONT><span id="wmqeeuq" class=postbody>HQL功能最强大Q适合各种情况Q但是动态条件查询构造v来很不方?<BR>Criteria最适合动态条件查询,不太适合l计查询QQBEq不够强大,只适合相当单的查询 <BR>NativeSQL可以实现特定数据库的SQLQ但是可UL性就牺牲?<BR><BR><BR>Hibernate2的Criteria功能不够完善Q所以Hibernate2上面可用的只有HQL和NativeSQLQHibernate3的Criteria已经非常强大了?<BR><BR>我的选择原则是: <BR><BR>针对web应用来说Q大部分常规查询都是动态条件查询,所以首选用CriteriaQƈ且Hibernate3提供了DetachedCriteriaQ可以在web层构造好DetachedCriteria再进入session执行查询Q非常方便实用的Ҏ?<BR><BR>但是涉及到统计查询和非常复杂的关联查询,Criteria无能ؓ力了Q这U情况下我选择使用HQL?<BR><BR>最后如果必M用某些数据库的特性,例如Oracle?...connect with ... by"q样的SQLQ则选择使用NativeSQL?/SPAN> <img src ="http://www.aygfsteel.com/jackybu/aggbug/9687.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/jackybu/" target="_blank">?/a> 2005-08-09 21:21 <a href="http://www.aygfsteel.com/jackybu/articles/9687.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ͨ</a>| <a href="http://" target="_blank">ְ</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">β</a>| <a href="http://" target="_blank">ʹ</a>| <a href="http://" target="_blank">ͭ</a>| <a href="http://" target="_blank">żҽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʵ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Į</a>| <a href="http://" target="_blank"> </a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ǿ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">γ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ר</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɿ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">տ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ǩ</a>| <a href="http://" target="_blank">ƺ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>