Hibernate learning notes 2
•
Java
Hibernate mapping
One to many, one to one, many to many
One to many mapping
Common attributes of the set element
<span style="color: #0000ff;">import<span style="color: #000000;"> org.hibernate.Session;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.hibernate.SessionFactory;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.hibernate.boot.registry.StandardServiceRegistry;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.hibernate.boot.registry.StandardServiceRegistryBuilder;
<span style="color: #0000ff;">import<span style="color: #000000;"> org.hibernate.cfg.Configuration;<span style="color: #0000ff;">public <span style="color: #0000ff;">class<span style="color: #000000;"> HibernateUtil {
<span style="color: #0000ff;">private <span style="color: #0000ff;">static<span style="color: #000000;"> SessionFactory sessionFactory;
<span style="color: #0000ff;">private <span style="color: #0000ff;">static<span style="color: #000000;"> Session session;</span><span style="color: #0000ff;">static</span><span style="color: #000000;"> { </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建Con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>uration对象 读取hibernate.cfg.cml<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>。完成初始化</span> Con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>uration con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>uration().con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>ure(); StandardServiceRegistryBuilder ssrb </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> StandardServiceRegistryBuilder(). applySettings(con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>.getProperties()); StandardServiceRegistry ssr </span>=<span style="color: #000000;"> ssrb.build(); sessionFactory </span>=<span style="color: #000000;"> con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>.buildSessionFactory(ssr); } </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>SessionFactory对象</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> SessionFactory getSessionFactory() { </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> sessionFactory; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>Session</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> Session getSession() { session </span>=<span style="color: #000000;"> sessionFactory.openSession(); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> session; } </span><span style="color: #008000;">//</span><span style="color: #008000;"> <a href="https://www.jb51.cc/tag/guanbi/" target="_blank" class="keywords">关闭</a>Session</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> closeSession(Session session) { </span><span style="color: #0000ff;">if</span> (session != <span style="color: #0000ff;">null</span><span style="color: #000000;">) { session.close(); } }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码