HIbernate_one-to-many

One-to-many:

One: master table, many: slave table
Primary key in primary table, foreign key in secondary table
one:
public class Customer {
	private Integer id;
	private String name;
	private Set<Order> orders = new HashSet<Order>();
many:
public class Order {
	private Integer id;
	private String name;
	private Customer customer;

  Configuration: 
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="domain">
    	<class name="Customer" table="t_customer">
    	<id name = "id" column="id">
    		<generator class="native"></generator>
    	</id>
    	<property name="name" column="name"></property>
    	<!-- A collection that expresses a one-to-many relationship
    		name: the property name of the collection
    	 -->
    	<set name="orders" inverse="false">
    	<!-- key: used to describe foreign keys
    		column: the value of the foreign key
    	 -->
    		<key column="cid"></key>
    		<!-- expresses that the relationship between Customer and Order is one-to-many
    			class: the full class name of the other party expressing the association
    		 -->
    		<one-to-many class="Order" />
    	</set>
    	</class>
    </hibernate-mapping>
    	

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="domain">
    	<class name="Order" table="t_order">
    	<id name = "id" column="id">
    		<generator class="native"></generator>
    	</id>
    	<property name="name" column="name"></property>
    	<!-- express many-to-one relationship
    		name: the referenced property name
    		column: the column name of the foreign key
    	 -->
    	<many-to-one name="customer" column="cid" class="Customer"></many-to-one>
    	</class>
    </hibernate-mapping>

test:
	@Test
	/**
	 * increase
	 */
	public void fun1() {
		//1. Read the configuration file
		Configuration con = new Configuration().configure();
		//2. Create a factory according to the configuration
		SessionFactory sf = con.buildSessionFactory();
		//3. Obtain the session object for operating the database through the factory
		Session session = sf.openSession();
		
		Transaction transaction = session.beginTransaction();
		//4. Operate the database
		Customer c=new Customer();
		c.setName("zhangsan");
		Order o1=new Order();
		o1.setName("123");
		Order o2=new Order();
		o2.setName("456");
		
		o1.setCustomer(c);
		o2.setCustomer(c);

		
		session.save(c);
		session.save(o1);
		session.save(o2);
		transaction.commit();
		//5. Close the resource
		session.close();
		sf.close();
	}

There is an inverse attribute in the set tag of the main table, the default value is false.
It means reverse, whether to reverse the maintenance of the relationship to the other party.
Set to true to give up maintaining foreign key relationships in Customer
<set name="orders" inverse="false">
    	<!-- key: used to describe foreign keys
    		column: the value of the foreign key
    	 -->
    		<key column="cid"></key>
    		<!-- expresses that the relationship between Customer and Order is one-to-many
    			class: the full class name of the other party expressing the association
    		 -->
    		<one-to-many class="Order" />
    	</set>
When to configure inverse?
Just look at the business, if the side of 1 often needs to maintain foreign keys, then do not configure the inverse attribute on the side of 1

There is also a cascade attribute in the set tag
cascade: cascade operation:

save-update : A saves and saves B at the same time

delete : delete A , delete B at the same time , AB does not exist

delete-orphan : delete orphans, cancel the relationship, and delete B at the same time , A exists.

If you need to configure multiple items, separate them with commas. <set cascade="save-update,delete">

all : save-update and delete integration

all-delete-orphan : three integrations






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325816855&siteId=291194637