Hibernate practice (2)
Original bromon Copyright
One-to-one relationship
In most systems, it is impossible to have only one data table. Otherwise, it violates the original intention of relational databases. The relationship between a table and a table is complex and can be divided into several situations:
● One-to-one Association)
● One-to-Multiple Association)
● Multi-to-one Association)
● Multi-to-many Association)
In order. Assume that an example of one-to-one association is:
Table: person
ID (primary key)
Name
Email Address
Table: Spouse
ID (foreign key)
Name
The person table stores user information, while spouse stores user spouse information. In general, the next person has only one spouse, which is suitable for one-on-one scenarios. If you are interested in the relationship, we can discuss this issue in the one-to-many and multi-to-one association. Maybe we can also find it in the many-to-many relationship ^_^ (animal !).
OK. Design pojo as follows:
The person 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 ing rules. This should be understandable:
<? XML version = "1.0"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 2.0 // en"
"'Target = _ blank> http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<Hibernate-mapping package = "org. bromon. zizz">
<Class name = "person" table = "person" lazy = "true">
<ID name = "ID" type = "integer" unsaved-value = "null">
<Column name = "ID" SQL-type = "int" not-null = "true"/>
<Generator class = "Identity"/>
</ID>
<Property name = "name" column = "name" not-null = "true" unique = "false"/>
<Property name = "email" column = "email" not-null = "false"/>
</Class>
</Hibernate-mapping>
So easy? Everything goes step by step. The following is the souse class:
/*
* Created on 2004-4-20
*/
Package org. bromon. zizz;
/**
* @ Author bromon
*/
Public class spouse
{
Private int ID;
Private string name;
Private person;
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)
{
This. Person = person;
}
Public Person getperson ()
{
Return (person );
}
}
Note the domain person in it. Its ing file:
<? XML version = "1.0"?>
<! Doctype hibernate-mapping public
"-// Hibernate/hibernate mapping DTD 2.0 // en"
"'Target = _ blank> http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<Hibernate-mapping package = "org. bromon. zizz">
<Class name = "spouse" table = "spouse" lazy = "true">
<ID name = "ID" type = "integer" unsaved-value = "null">
<Column name = "ID" SQL-type = "int" not-null = "true"/>
<Generator class = "foreign">
<Param name = "property"> person </param>
</Generator>
</ID>
<Property name = "name" column = "name" not-null = "true" unique = "false"/>
<One-to-one name = "person" class = "person" cascade = "all" constrained = "true"/>
</Class>
</Hibernate-mapping>
The generator of ID is a foreign key and associated with person. It is difficult to understand whether to specify a one-to-one relationship? Hibernate is indeed in line with our habits of thinking. It should be noted that this association is one-way, and people do not need to specify spouse.
Perform the following operations on these 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
{
Configuration 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 = new person ();
Person. setname ("bromon ");
Person. setemail ("bromon@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 article. OK. Run the command and check the zizz database.