Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Hibernate one-to-many

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to achieve Hibernate one-to-many". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve Hibernate one-to-many"!

First look at the one-to-many configuration instructions translated by the Manjianghong translation team (RedSaga Translate Team), and then look at the examples

One to many association (One-to-many Associations)

An one-to-many association joins the corresponding tables of two classes through foreign keys without an intermediate collection table. This relational model loses some of the semantics of Java collections:

An instance of a contained entity can only be contained in an instance of a collection.

An instance of an included entity can only correspond to one value in the collection index

An association from Product to Part requires a key field, and there may be an index field that points to the table corresponding to Part. The tag indicates an one-to-many association.

(1) class (required): the name of the associated class.

(2) not-found (optional-default is exception): indicates what to do if the row associated with the cached tag value is missing: ignore treats the missing row as an empty association.

(3) entity-name (optional): the entity name of the associated class, as an alternative to class.

Examples

Set >

Note: the element does not need to define any fields. There is no need to specify a table name.

Important hint

If the foreign key field in the one-to-many association of the Hibernate instance is defined as NOT NULL, you must declare the mapping as not-null= "true", or use a two-way association and mark inverse= "true".

1 build the table first

Create table student (sid varchar (32) not null primary key, sname varchar (16), sage varchar (16),) create table book (bid varchar (32) not null primary key, bname varchar (16), bprice varchar (16), sid varchar

two。 Write vo Student.java

Package com.test; import java.util.Set; public class Student {private String sid; private String sname; private String sage; private Set book; public Student () {} / / write get set

Book.JAVA

Package com.test; public class Book {private String bid; private String bname; private String bprice; public Book () {} / / write get set

3. Write the corresponding mapping file Student.hbm.xml

PUBLIC "- / Hibernate/Hibernate Mapping DTD//EN"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> id > property > property > set > class > hibernate-mapping >

Book.hbm.xml

PUBLIC "- / Hibernate/Hibernate Mapping DTD//EN"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> id > property > property > class > hibernate-mapping >

Then copy the following hibernate.properties file to the classes directory. Mysql is used here.

Hibernate.query.substitutions true 1, false 0, yes'Yee, no 'N' # # MySQL hibernate.dialect net.sf.hibernate.dialect.MySQLDialect hibernate.connection.driver_class org.gjt.mm.mysql.Driver hibernate.connection.url jdbc:mysql://localhost:3306/wjcms hibernate.connection.username root hibernate.connection.password wujun hibernate.connection.pool_size 1 hibernate.proxool.pool_alias pool1 hibernate.show_sql true hibernate.jdbc.batch_size 0 hibernate.max_fetch_depth 1 hibernate.cache.use_query_cache true

4. Write the test class..

Package com.test; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import net.sf.hibernate.*; import java.util.Set; import java.util.HashSet; import java.sql.*; import java.util.List; import java.util.Iterator; public class TestOneToMany {SessionFactory sf; Session session Public TestOneToMany () {try {Configuration cfg = new Configuration (); sf = cfg.addClass (Student.class) .addClass (Book.class). BuildSessionFactory ();} catch (HibernateException ex) {ex.printStackTrace () }} / / insert public void doCreate () {try {session = sf.openSession (); Student student = new Student (); student.setSname ("Xiao Wang"); student.setSage ("22"); Set bookSet = new HashSet (); Book book = null For (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report