Hibernate关联关系映射(单向篇)
Hibernate关联关系可分为单向关联和双向关联两大类。单向关联可以分为一对一、一对多、多对一和多对多4种关联方式,而多向关联可以分为一对一、一对多和多对多3种关联方式。 Hibernate连接管理类HibernateUtil.java public class HibernateUtil { private st
Hibernate关联关系可分为单向关联和双向关联两大类。单向关联可以分为一对一、一对多、多对一和多对多4种关联方式,而多向关联可以分为一对一、一对多和多对多3种关联方式。
Hibernate连接管理类HibernateUtil.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> HibernateUtil { <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> SessionFactory sessionFactory; <span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> final ThreadLocal<Session> threadLocal = <span class="hljs-keyword">new</span> ThreadLocal<Session>(); <span class="hljs-keyword">static</span>{ <span class="hljs-keyword">try</span>{ Configuration cfg = <span class="hljs-keyword">new</span> Configuration().configure(); sessionFactory = cfg.buildSessionFactory(); }<span class="hljs-keyword">catch</span>(Throwable ex){ <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> ExceptionInInitializerError(ex); } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> SessionFactory <span class="hljs-title">getSessionFactory</span>(){ <span class="hljs-keyword">return</span> sessionFactory; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> Session <span class="hljs-title">getSession</span>() throws HibernateException{ Session session = (Session)threadLocal.<span class="hljs-keyword">get</span>(); <span class="hljs-keyword">if</span>(session == <span class="hljs-keyword">null</span> || !session.isOpen()){ session = (sessionFactory != <span class="hljs-keyword">null</span>)?sessionFactory.openSession():<span class="hljs-keyword">null</span>; threadLocal.<span class="hljs-keyword">set</span>(session); } <span class="hljs-keyword">return</span> session; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">closeSession</span>() throws HibernateException{ Session session = (Session)threadLocal.<span class="hljs-keyword">get</span>(); threadLocal.<span class="hljs-keyword">set</span>(<span class="hljs-keyword">null</span>); <span class="hljs-keyword">if</span>(session != <span class="hljs-keyword">null</span>){ session.close(); } } <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">shutdown</span>(){ getSessionFactory().close(); } }</code>
1.单向的一对一关联
(1)通过主键关联
通过主键关联,是指两个数据表之间通过主键建立一对一的关联关系。这两张表的主键值是相同的,一张表改动时,另一张也会相关的发生改变,从而避免多余字段被创建,但基于主键关联的持久化类(其对应的数据表称为从表)不能拥有自己的主键生成策略,它的主键由关联类负责生成。另外,增加one-to-one元素来关联属性,必须为one-to-one元素增加constrained=”true”属性,表明该类主键由关联类生成。
示例:
实体类:
User.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid; <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String password; <span class="hljs-keyword">private</span> Address address; <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() { <span class="hljs-keyword">return</span> userid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) { <span class="hljs-keyword">this</span>.userid = userid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) { <span class="hljs-keyword">this</span>.name = name; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() { <span class="hljs-keyword">return</span> password; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) { <span class="hljs-keyword">this</span>.password = password; } <span class="hljs-keyword">public</span> Address <span class="hljs-title">getAddress</span>() { <span class="hljs-keyword">return</span> address; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Address address) { <span class="hljs-keyword">this</span>.address = address; } }</code>
Address.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid; <span class="hljs-keyword">private</span> String addressinfo; <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() { <span class="hljs-keyword">return</span> addressid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) { <span class="hljs-keyword">this</span>.addressid = addressid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() { <span class="hljs-keyword">return</span> addressinfo; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) { <span class="hljs-keyword">this</span>.addressinfo = addressinfo; } }</code>
表结构:
user表:
address表:
配置文件:
User.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"id"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"user_id"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"foreign"</span>> <param <span class="hljs-property">name</span>=<span class="hljs-string">"property"</span>>address</param> </generator> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>> </<span class="hljs-keyword">property</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>> </<span class="hljs-keyword">property</span>> <one-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"Address"</span> constrained=<span class="hljs-string">"true"</span>></one-<span class="hljs-keyword">to</span>-one> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
Address.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>> </<span class="hljs-keyword">property</span>> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
测试类Test.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Test { <span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span>(String[] args){ Session session = HibernateUtil.getSession(); Transaction tx = session.beginTransaction(); User u = <span class="hljs-keyword">new</span> User(); u.setName(<span class="hljs-string">"sbw"</span>); u.setPassword(<span class="hljs-string">"123"</span>); Address a = <span class="hljs-keyword">new</span> Address(); a.setAddressinfo(<span class="hljs-string">"heu"</span>); u.setAddress(a); session.save(a); session.save(u); tx.commit(); HibernateUtil.closeSession(); } }</code>
运行结果:
(2)通过外键关联
通过外键关联时两张数据表的主键是不同的,通过在一张表中添加外键列来保持一对一的关系。配置外键关联关系时需要使用many-to-one元素。因为通过外键关联的一对一关系,本质上是多对一关系的特例。因此,只需在many-to-one元素中增加unique=“true”属性即可,这相当于在多的一端增加了唯一性的约束,表示多的一端也必须是唯一的,这样就变成为单向的一对一关系了。
示例:
实体类与通过主键关联相同。
表结构:
user表
address表
配置文件:
User.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>> </<span class="hljs-keyword">property</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>> </<span class="hljs-keyword">property</span>> <many-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.Address"</span> unique=<span class="hljs-string">"true"</span> column=<span class="hljs-string">"addressid"</span>></many-<span class="hljs-keyword">to</span>-one> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
Address.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>> </<span class="hljs-keyword">property</span>> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
测试类与通过主键关联一样
运行结果:
2.单向一对多关联
单向的一对多关联映射关系主要是通过外键来关联的。一对多的关联映射是在表示多的一方的数据表中增加一个外键,并由“一”的一方指向“多”的一方。单向的一对多关联的持久化类里需要包含一个集合属性,在“一”的一方访问“多”的一方时,“多”的一方将以集合的形式来体现。
示例:
实体类:
User.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid; <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String password; <span class="hljs-keyword">private</span> Set<Address> address = <span class="hljs-keyword">new</span> HashSet<Address>(); <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() { <span class="hljs-keyword">return</span> userid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) { <span class="hljs-keyword">this</span>.userid = userid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) { <span class="hljs-keyword">this</span>.name = name; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() { <span class="hljs-keyword">return</span> password; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) { <span class="hljs-keyword">this</span>.password = password; } <span class="hljs-keyword">public</span> Set<Address> <span class="hljs-title">getAddress</span>() { <span class="hljs-keyword">return</span> address; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Set<Address> address) { <span class="hljs-keyword">this</span>.address = address; } }</code>
Address.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid; <span class="hljs-keyword">private</span> String addressinfo; <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() { <span class="hljs-keyword">return</span> addressid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) { <span class="hljs-keyword">this</span>.addressid = addressid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() { <span class="hljs-keyword">return</span> addressinfo; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) { <span class="hljs-keyword">this</span>.addressinfo = addressinfo; } }</code>
表结构:
User表
Address表
配置文件
User.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>> </<span class="hljs-keyword">property</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>> </<span class="hljs-keyword">property</span>> <<span class="hljs-keyword">set</span> <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> table=<span class="hljs-string">"address"</span> inverse=<span class="hljs-string">"false"</span> lazy=<span class="hljs-string">"true"</span>> <key> <column <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span>/> </key> <one-<span class="hljs-keyword">to</span>-many <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.User"</span>/> </<span class="hljs-keyword">set</span>> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
Address.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>> </<span class="hljs-keyword">property</span>> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
测试代码
Test.java
<code class=" hljs avrasm">public class Test { public static void main(String[] args){ Session session = HibernateUtil<span class="hljs-preprocessor">.getSession</span>()<span class="hljs-comment">;</span> Transaction tx = session<span class="hljs-preprocessor">.beginTransaction</span>()<span class="hljs-comment">;</span> User u = new User()<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"sbw"</span>)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"123"</span>)<span class="hljs-comment">;</span> Address a = new Address()<span class="hljs-comment">;</span> a<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"heu"</span>)<span class="hljs-comment">;</span> Address a1 = new Address()<span class="hljs-comment">;</span> a1<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"hrb"</span>)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a1)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(a)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(a1)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(u)<span class="hljs-comment">;</span> tx<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;</span> HibernateUtil<span class="hljs-preprocessor">.closeSession</span>()<span class="hljs-comment">;</span> } }</code>
运行结果:
3.单向的多对一关联
单向的多对一关联映射关系也是通过外键来关联的。多对一的映射方式类似于一对多的映射方式,不过它的映射关系是由“多”的一方指向“一”的一方。在表示“多”的一方的数据表中增加一个外键来指向表示“一”的一方的数据表,“一”的一方作为主表,”多“的一方作为从表。
示例:
实体类
User.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid; <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String password; <span class="hljs-keyword">private</span> Address address = <span class="hljs-keyword">new</span> Address(); <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() { <span class="hljs-keyword">return</span> userid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) { <span class="hljs-keyword">this</span>.userid = userid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) { <span class="hljs-keyword">this</span>.name = name; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() { <span class="hljs-keyword">return</span> password; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) { <span class="hljs-keyword">this</span>.password = password; } <span class="hljs-keyword">public</span> Address <span class="hljs-title">getAddress</span>() { <span class="hljs-keyword">return</span> address; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Address address) { <span class="hljs-keyword">this</span>.address = address; } }</code>
Address.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid; <span class="hljs-keyword">private</span> String addressinfo; <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() { <span class="hljs-keyword">return</span> addressid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) { <span class="hljs-keyword">this</span>.addressid = addressid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() { <span class="hljs-keyword">return</span> addressinfo; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) { <span class="hljs-keyword">this</span>.addressinfo = addressinfo; } }</code>
表结构:
user表
address表
配置文件:
User.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>> </<span class="hljs-keyword">property</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>> </<span class="hljs-keyword">property</span>> <many-<span class="hljs-keyword">to</span>-one <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> column=<span class="hljs-string">"addressid"</span>/> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
Address.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>> </<span class="hljs-keyword">property</span>> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
测试类Test.java
<code class=" hljs avrasm">public class Test { public static void main(String[] args){ Session session = HibernateUtil<span class="hljs-preprocessor">.getSession</span>()<span class="hljs-comment">;</span> Transaction tx = session<span class="hljs-preprocessor">.beginTransaction</span>()<span class="hljs-comment">;</span> User u = new User()<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"sbw"</span>)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"123"</span>)<span class="hljs-comment">;</span> User u1 = new User()<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"gaoya"</span>)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"456"</span>)<span class="hljs-comment">;</span> Address a = new Address()<span class="hljs-comment">;</span> a<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"heu"</span>)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setAddress</span>(a)<span class="hljs-comment">;</span> u1<span class="hljs-preprocessor">.setAddress</span>(a)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(a)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(u)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(u1)<span class="hljs-comment">;</span> tx<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;</span> HibernateUtil<span class="hljs-preprocessor">.closeSession</span>()<span class="hljs-comment">;</span> } }</code>
测试结果:
4.单向的多对多关联
多对多关联在数据库中是比较常见的,它利用中间表将两个主表关联起来。中间表的作用是将两张表的主键作为其外键,通过外键建立这两张表的映射关系。在单向的多对多关联中,需要在主控端的类定义中增加一个Set集合属性,使得被关联一方的类的实例以集合的形式存在。
示例:
实体类:
User.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> User { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> userid; <span class="hljs-keyword">private</span> String name; <span class="hljs-keyword">private</span> String password; <span class="hljs-keyword">private</span> Set<Address> address = <span class="hljs-keyword">new</span> HashSet<Address>(); <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getUserid</span>() { <span class="hljs-keyword">return</span> userid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setUserid</span>(<span class="hljs-keyword">int</span> userid) { <span class="hljs-keyword">this</span>.userid = userid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getName</span>() { <span class="hljs-keyword">return</span> name; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setName</span>(String name) { <span class="hljs-keyword">this</span>.name = name; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getPassword</span>() { <span class="hljs-keyword">return</span> password; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setPassword</span>(String password) { <span class="hljs-keyword">this</span>.password = password; } <span class="hljs-keyword">public</span> Set<Address> <span class="hljs-title">getAddress</span>() { <span class="hljs-keyword">return</span> address; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddress</span>(Set<Address> address) { <span class="hljs-keyword">this</span>.address = address; } }</code>
Address.java
<code class=" hljs cs"><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> Address { <span class="hljs-keyword">private</span> <span class="hljs-keyword">int</span> addressid; <span class="hljs-keyword">private</span> String addressinfo; <span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">getAddressid</span>() { <span class="hljs-keyword">return</span> addressid; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressid</span>(<span class="hljs-keyword">int</span> addressid) { <span class="hljs-keyword">this</span>.addressid = addressid; } <span class="hljs-keyword">public</span> String <span class="hljs-title">getAddressinfo</span>() { <span class="hljs-keyword">return</span> addressinfo; } <span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">setAddressinfo</span>(String addressinfo) { <span class="hljs-keyword">this</span>.addressinfo = addressinfo; } }</code>
表结构:
user表:
user_address表:
address表:
配置文件:
User.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.User"</span> table=<span class="hljs-string">"user"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"userid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"userid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"name"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"name"</span>> </<span class="hljs-keyword">property</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"password"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"password"</span>> </<span class="hljs-keyword">property</span>> <<span class="hljs-keyword">set</span> <span class="hljs-property">name</span>=<span class="hljs-string">"address"</span> table=<span class="hljs-string">"user_address"</span>> <key column=<span class="hljs-string">"userid"</span>/> <many-<span class="hljs-keyword">to</span>-many column=<span class="hljs-string">"addressid"</span> <span class="hljs-type">class</span>=<span class="hljs-string">"com.entity.Address"</span>/> </<span class="hljs-keyword">set</span>> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
Address.hbm.xml
<code class=" hljs applescript"><hibernate-mapping> <<span class="hljs-type">class</span> <span class="hljs-property">name</span>=<span class="hljs-string">"com.entity.Address"</span> table=<span class="hljs-string">"address"</span>> <<span class="hljs-property">id</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressid"</span> type=<span class="hljs-string">"java.lang.Integer"</span> column=<span class="hljs-string">"addressid"</span>> <generator <span class="hljs-type">class</span>=<span class="hljs-string">"identity"</span>/> </<span class="hljs-property">id</span>> <<span class="hljs-keyword">property</span> <span class="hljs-property">name</span>=<span class="hljs-string">"addressinfo"</span> type=<span class="hljs-string">"java.lang.String"</span> column=<span class="hljs-string">"addressinfo"</span>> </<span class="hljs-keyword">property</span>> </<span class="hljs-type">class</span>> </hibernate-mapping></code>
测试代码Test.java
<code class=" hljs avrasm">public class Test { public static void main(String[] args){ Session session = HibernateUtil<span class="hljs-preprocessor">.getSession</span>()<span class="hljs-comment">;</span> Transaction tx = session<span class="hljs-preprocessor">.beginTransaction</span>()<span class="hljs-comment">;</span> User u = new User()<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"sbw"</span>)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"123"</span>)<span class="hljs-comment">; </span> User u1 = new User()<span class="hljs-comment">;</span> u1<span class="hljs-preprocessor">.setName</span>(<span class="hljs-string">"gaoya"</span>)<span class="hljs-comment">;</span> u1<span class="hljs-preprocessor">.setPassword</span>(<span class="hljs-string">"456"</span>)<span class="hljs-comment">;</span> Address a = new Address()<span class="hljs-comment">;</span> a<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"heu"</span>)<span class="hljs-comment">;</span> Address a1 = new Address()<span class="hljs-comment">;</span> a1<span class="hljs-preprocessor">.setAddressinfo</span>(<span class="hljs-string">"hrb"</span>)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a)<span class="hljs-comment">;</span> u<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a1)<span class="hljs-comment">;</span> u1<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a)<span class="hljs-comment">;</span> u1<span class="hljs-preprocessor">.getAddress</span>()<span class="hljs-preprocessor">.add</span>(a1)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(a)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(a1)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(u)<span class="hljs-comment">;</span> session<span class="hljs-preprocessor">.save</span>(u1)<span class="hljs-comment">;</span> tx<span class="hljs-preprocessor">.commit</span>()<span class="hljs-comment">;</span> HibernateUtil<span class="hljs-preprocessor">.closeSession</span>()<span class="hljs-comment">;</span> } }</code>
测试结果:

Heiße KI -Werkzeuge

Undresser.AI Undress
KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover
Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool
Ausziehbilder kostenlos

Clothoff.io
KI-Kleiderentferner

AI Hentai Generator
Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

Heiße Werkzeuge

Notepad++7.3.1
Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version
Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1
Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6
Visuelle Webentwicklungstools

SublimeText3 Mac-Version
Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Heiße Themen

Hibernate in das SpringBoot-Projekt integrieren Vorwort Hibernate ist ein beliebtes ORM-Framework (Object Relational Mapping), das Java-Objekte Datenbanktabellen zuordnen kann, um Persistenzvorgänge zu erleichtern. Im SpringBoot-Projekt kann uns die Integration von Hibernate dabei helfen, Datenbankoperationen einfacher durchzuführen. In diesem Artikel wird die Integration von Hibernate in das SpringBoot-Projekt vorgestellt und entsprechende Beispiele bereitgestellt. 1.Abhängigkeiten einführenFügen Sie die folgenden Abhängigkeiten in die Datei pom.xml ein: org.springframework.bootspring-boot-starter-data-jpam

Was ist JPA? Wie unterscheidet es sich von JDBC? JPA (JavaPersistence API) ist eine Standardschnittstelle für objektrelationales Mapping (ORM), die es Java-Entwicklern ermöglicht, vertraute Java-Objekte zum Betreiben von Datenbanken zu verwenden, ohne SQL-Abfragen direkt in die Datenbank schreiben zu müssen. JDBC (JavaDatabaseConnectivity) ist die Standard-API von Java für die Verbindung mit Datenbanken. Sie erfordert, dass Entwickler SQL-Anweisungen verwenden, um die Datenbank zu betreiben. JPA kapselt JDBC, bietet eine bequemere und übergeordnete API für die objektrelationale Zuordnung und vereinfacht Datenzugriffsvorgänge. Was ist in JPA eine Entität? juristische Person

Java ist eine objektorientierte Programmiersprache, die im Bereich der Softwareentwicklung weit verbreitet ist. Hibernate ist ein beliebtes Java-Persistenz-Framework, das eine einfache und effiziente Möglichkeit bietet, die Persistenz von Java-Objekten zu verwalten. Während des Entwicklungsprozesses treten jedoch häufig Hibernate-Fehler auf, die dazu führen können, dass das Programm abnormal beendet wird oder instabil wird. Der Umgang mit und die Vermeidung von Hibernate-Fehlern ist zu einer Fähigkeit geworden, die Java-Entwickler beherrschen müssen. In diesem Artikel werden einige gängige Hib vorgestellt

Wie ordne ich MySQL-Fremdschlüssel und Primärschlüssel automatisch zu? In der MySQL-Datenbank sind Fremdschlüssel und Primärschlüssel sehr wichtige Konzepte. Sie können uns dabei helfen, Beziehungen zwischen verschiedenen Tabellen herzustellen und die Integrität und Konsistenz der Daten sicherzustellen. In tatsächlichen Anwendungsprozessen ist es häufig erforderlich, Fremdschlüssel automatisch den entsprechenden Primärschlüsseln zuzuordnen, um Dateninkonsistenzen zu vermeiden. Im Folgenden wird anhand spezifischer Codebeispiele erläutert, wie diese Funktion implementiert wird. Zuerst müssen wir zwei Tabellen erstellen, eine als Master-Tabelle und die andere als Slave-Tabelle. In der Haupttabelle erstellen

Das Android-System und der Linux-Kernel sind zwei eng verwandte Einheiten, und die Beziehung zwischen ihnen ist eng und komplex. Im Android-System spielt der Linux-Kernel eine wichtige Rolle, da er zugrunde liegende Hardwaretreiber und Systemaufrufunterstützung für das Android-System bereitstellt. In diesem Artikel wird die Beziehung zwischen dem Android-System und dem Linux-Kernel untersucht, wie sie interagieren und zusammenarbeiten, und einige spezifische Codebeispiele bereitstellen. Android ist ein mobiles Betriebssystem, das auf Basis des Linux-Kernels entwickelt wurde und hauptsächlich für mobile Geräte wie Smartphones und Tablets verwendet wird. L

Die Unterschiede zwischen Hibernate und Mybatis: 1. Implementierungsmethode; 3. Vergleich der Objektverwaltung; Detaillierte Einführung: 1. Implementierungsmethode: Hibernate ist eine vollständige Objekt-/Relational-Mapping-Lösung, die Objekte Datenbanktabellen zuordnet, während MyBatis von Entwicklern das manuelle Schreiben von SQL-Anweisungen und ResultMap erfordert. 2. Leistung: Hibernate ist in Bezug auf die Entwicklungsgeschwindigkeit schneller MyBatis, weil Hibernate die DAO-Ebene usw. vereinfacht.

Forschung zur Beziehung zwischen Huawei Hongmeng-System und Android Mit der kontinuierlichen Weiterentwicklung der Technologie sind Smartphones zu einem unverzichtbaren Bestandteil des Lebens der Menschen geworden. Als einer der weltweit führenden Mobiltelefonhersteller arbeitet Huawei ständig an Innovationen und ist bestrebt, bessere mobile Betriebssysteme und Benutzererlebnisse bereitzustellen. In den letzten Jahren begann Huawei mit der Unterdrückung von Huawei durch die USA, die Entwicklung seines eigenen Betriebssystems zu beschleunigen, und HarmonyOS entstand. In diesem Zusammenhang haben die Menschen begonnen, der Beziehung zwischen dem Hongmeng-System und Android Aufmerksamkeit zu schenken. Zuerst müssen wir verstehen

Der „C“-Compiler wertet Ausdrücke nach Prioritäts- und Assoziativitätsregeln aus. Wenn ein Ausdruck Operatoren unterschiedlicher Priorität enthält, werden Prioritätsregeln berücksichtigt. Hier wird 10*2 zuerst ausgewertet, da „*“ eine höhere Priorität hat als „-“ und „=“. Wenn die Ausdrücke die gleiche Priorität haben, wird die Assoziativitätsregel berücksichtigt, d. h. von links nach rechts (oder von rechts nach links). ).
