Hibernate actual combat (2)

zhaozj2021-02-16  38

Hibernate actual combat (2)

Bromon original copyright

One-to-one relationship

In most systems, it is impossible to exist only a data table, otherwise it is contrary to the original intention of the relational database. The relationship between the table and the table is more complicated, which can be divided into several situations:

● One to one ● One to many "● Many to one ● Many-to-many associations (MANY TO MANY)

In order of order. Suppose a one-to-one correlation is: Table: Person ID Number (primary key) NAME Name Email Email Address

Form: SPOUSE ID number (foreign key) NAME name

Person This table saves user information, and SPOUSE saves the information of the user spouse. In general, only one spouse is in general, which is very suitable for our one-on-one situation. If you are interested in marriage, we can discuss this problem in a pair of and more-to-one associations, perhaps you can still be more-to-middle ^ _ ^ (animals!).

OK, below design POJO: Person This class is very simple:

/ * * CREATED ON 2004-4-19 * / package Org.bromon.zizz;

/ ** * @Author bromon * / public class person {private int id; private string name; private string email;

Public void setid (int id) {this.id = id;} public int getId () {return (id);

Public void setname (String name) {this.name = name;} public string getname () {return (name);

Public void setemail (string email) {this.email = email;} public string geteMail () {return (email);}}

Then write its mapping rules, this should be able to understand: >

SO EASY is? Everything will work according to the department. Below is a SOUSE class:

/ * * CREATED ON 2004-4-20 * / package org.bromon.zizz;

/ ** * @Author bromon * / public class spouse {private int id; private string name; private persot

Public void setid (int id) {this.id = id;} public int getId () {return (id);

Public void setname (String name) {this.name = name;} public string getname () {return (name);

Public void setPerson (Person Person) {this.Person = Person;} PUBLIC PERSON getPerson () {Return (Person);

Pay attention to Person in the domain. Its mapping file:

http://hibernate.sourceforge.net/hibernate-mapping -2.0.dtd ">

Here, the recognitive generator is an foreign key, associated with Person. Then specify one one-to-one relationship, it is not difficult to understand? Hibernate is really in line with our thinking habits. What needs to be reminded is that this association relationship is unidirectional, and Person does not need to specify the spouse.

Let's take two classes:

/ * * Created on 2004-4-20 * / package org.bromon.zizz; import net.sf.hibernate. *; Import net.sf.hibernate.cfg. *; Import net.sf.hibernate.tool.hbm2ddl. *; / ** * @Author bromon * / public class operatespouse {public static void main (String args []) {Try {configure cfg = new configuration (). Addclass (spouse.class); cfg.addclass (person.class ); SessionFactory factory = cfg.buildsessionFactory (); new schemaExport (cfg) .create (true, true); session session = factory.opensession (); person: "); person .setname (" bromon "); Person.setemail ("broman@163.com"); spouse spouse = new spouse (); spouse.setname ("who"); spouse.setPerson (Person); Transaction TS = session.begintransaction (); session.save Person); session.save (spouse); ts.commit (); session.close ();} catch (exception e) {system.out.println (e);}}}

This example is very similar to the example in the first one. OK, execute it, then look at the zizz database, and

转载请注明原文地址:https://www.9cbs.com/read-21726.html

New Post(0)