Hibernate ---- N-1 (1), hibernate ---- N-1 (

Source: Internet
Author: User

Hibernate ---- N-1 (1), hibernate ---- N-1 (

* *********************** 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>

<Hibernate-configuration>

<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>

</Hibernate-configuration>

 

 

 

 

 

**************************************** ***********************

Package com. ij34.dao;

Import java. util. HashSet;
Import java. util. Set;

Import javax. persistence .*;

@ Entity
@ Table (name = "lele_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 object
@ Onetoty (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 age ){
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)
// Use the composite primary key
@ JoinColumns ({
@ JoinColumn (name = "lele_first", referencedColumnName = "first", nullable = false ),
@ JoinColumn (name = "lele_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 Configuration
Configuration conf = new Configuration (). configure ();
ServiceRegistry SR = new StandardServiceRegistryBuilder (). applySettings (conf. getProperties (). build ();
// Create 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 the value for the two members of the composite primary key
People people = new People ();
People. setAge (22 );
People. setFirst ("surnamed Lin ");
People. setLast (" ");
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 ();
}
}

**************************************** ************************

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.