Hibernate.cfg.xml
<?xml version= ' 1.0 ' encoding= ' utf-8 '?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" >
<session-factory>
<!--Database connection Settings--
<property name= "Connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Connection.url" >jdbc:mysql://localhost:3306/hibernate</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >123456</property>
<!--SQL dialect--
<property name= "dialect" >org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!--Echo all executed SQL to stdout
<property name= "Show_sql" >true</property>
<!--Drop and re-create the database schema on startup--
<property name= "Hbm2ddl.auto" >create</property>
<!--Names The annotated entity class--
<mapping class= "Com.ij34.dao.Address"/>
<mapping class= "Com.ij34.dao.People"/>
</session-factory>
***************************************************************
Package Com.ij34.dao;
Import Java.util.HashSet;
Import Java.util.Set;
Import javax.persistence.*;
@Entity
@Table (name= "People_inf")
Public class people implements java.io.serializable{
Private static final long serialversionuid = 1L;
@Id
Private String first;
@Id
Private String last;
private int age;
Record all address entities associated with the people entity
@OneToMany (targetentity=address.class,mappedby= "People", Cascade=cascadetype.all)
Private set<address> address=new hashset<> ();
Public String GetFirst () {
return first;
}
public void Setfirst (String first) {
This.first = First;
}
Public String GetLast () {
return last;
}
public void Setlast (String last) {
This.last = Last;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public set<address> getaddress () {
return address;
}
public void setaddress (set<address> Address) {
this.address = address;
}
}
**************************************************************************
Package Com.ij34.dao;
Import javax.persistence.*;
@Entity
@Table (name= "Address_inf")
public class address{
@Id @Column (name= "address_id")
@GeneratedValue (strategy=generationtype.identity)
private int addressid;
Private String message;
@ManyToOne (Targetentity=people.class)
Using composite primary keys
@JoinColumns ({
@JoinColumn (name= "People_first", referencedcolumnname= "first", Nullable=false),
@JoinColumn (name= "People_last", Referencedcolumnname= "last", Nullable=false)
})
Private people people;
public int Getaddressid () {
return addressid;
}
public void Setaddressid (int addressid) {
This.addressid = Addressid;
}
Public String GetMessage () {
return message;
}
public void Setmessage (String message) {
this.message = message;
}
Public People getPeople () {
return people;
}
public void Setpeople (people people) {
This.people = people;
}
}
**********************************************************************************
Package com.ij34.web;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import org.hibernate.Transaction;
Import Org.hibernate.boot.registry.StandardServiceRegistryBuilder;
Import org.hibernate.cfg.Configuration;
Import org.hibernate.service.*;
Import com.ij34.dao.Address;
Import Com.ij34.dao.People;
public class Test01 {
public static void Main (string[] args) throws Exception {
Instantiate a configuration
Configuration Conf=new configuration (). Configure ();
Serviceregistry sr=new Standardserviceregistrybuilder (). Applysettings (Conf.getproperties ()). build ();
Creating a Sessionfactory instance with a configuration instance
Sessionfactory sf=conf.buildsessionfactory (SR);
Create session
Session session=sf.opensession ();
Start transaction
Transaction tx=session.begintransaction ();
People person = new people ();
Person.setage (29);
Set values for two members of a composite primary key
People people=new people ();
People.setage (22);
People.setfirst ("surname Lin");
People.setlast ("The famous Puma");
Address A1=new address ();
A1.setmessage ("Guangzhou");
A1.setpeople (people);
Address A2=new address ();
A2.setmessage ("Maoming");
A2.setpeople (people);
Session.save (people);
Session.save (A1);
Session.save (A2);
Tx.commit ();
Session.close ();
Sf.close ();
}
}
****************************************************************
Hibernate----N-1 (i)