程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> hibernate----N-1(一),hibernate----N-1(

hibernate----N-1(一),hibernate----N-1(

編輯:JAVA綜合教程

hibernate----N-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="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;
// 記錄該People實體關聯的所有Address實體
@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 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)
// 使用復合主鍵
@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 {
//實例化Configuration
Configuration conf=new Configuration().configure();
ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
// 以Configuration實例創建SessionFactory實例
SessionFactory SF=conf.buildSessionFactory(SR);
//create session
Session session=SF.openSession();
//start 事務
Transaction tx=session.beginTransaction();
People person = new People();
person.setAge(29);
// 為復合主鍵的兩個成員設置值
People people=new People();
people.setAge(22);
people.setFirst("姓林");
people.setLast("名彪");
Address a1=new Address();
a1.setMessage("廣州");
a1.setPeople(people);
Address a2=new Address();
a2.setMessage("茂名");
a2.setPeople(people);
session.save(people);
session.save(a1);
session.save(a2);
tx.commit();
session.close();
SF.close();
}
}

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

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved