类图:
![](http://img.e-com-net.com/image/info8/688c29f6b94343f5ab9cb14dca36c4cf.jpg)
代码:
Salesman.java:
- package basicCar.bean;
-
- import java.util.HashSet;
- import java.util.Set;
-
- import javax.persistence.AttributeOverride;
- import javax.persistence.AttributeOverrides;
- import javax.persistence.CascadeType;
- import javax.persistence.Column;
- import javax.persistence.Embedded;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.JoinTable;
- import javax.persistence.ManyToMany;
- import javax.persistence.OneToMany;
- import javax.persistence.OneToOne;
- import javax.persistence.Table;
-
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- import org.hibernate.annotations.GenericGenerator;
-
-
- @Entity
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- @Table(name = "salesman")
- public class Salesman implements java.io.Serializable {
- @Id
- @GeneratedValue(generator = "mysqlIncrement")
- @GenericGenerator(name = "mysqlIncrement", strategy = "increment")
- private long sid;
-
-
- @Column
- private String salesName;
-
- @OneToOne(mappedBy="salesman",cascade=CascadeType.ALL)
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- private Alias alias;
-
-
-
- @OneToMany(mappedBy="salesman",cascade = CascadeType.ALL)
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- private Set carOrders = new HashSet();
-
- @ManyToMany(
- targetEntity=basicCar.bean.BasicCar.class,
- cascade = CascadeType.ALL
- )
- @JoinTable(
- name = "carorder",
- joinColumns = { @JoinColumn(name = "salesId")} ,
- inverseJoinColumns={@JoinColumn(name="carId")}
- )
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- private Set cars = new HashSet();
-
- @Embedded
- @AttributeOverrides({
- @AttributeOverride(name="province", column=@Column(name="province")),
- @AttributeOverride(name="city", column=@Column(name="city")),
- @AttributeOverride(name="street", column=@Column(name="street")),
- @AttributeOverride(name="number", column=@Column(name="number"))
- })
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- private Address address;
-
-
-
-
-
-
-
-
-
-
- public Salesman() {
- }
-
- public Salesman(String N) {
- this.salesName = N;
- }
-
- public long getSid() {
- return this.sid;
- }
-
- private void setSid(long sid) {
- this.sid = sid;
- }
-
- public String getSalesname() {
- return this.salesName;
- }
-
- public void setSalesname(String salesName) {
- this.salesName = salesName;
- }
-
- public void setAddress(Address address) {
- this.address = address;
- }
-
- public Address getAddress() {
- return address;
- }
-
- public void setCarOrders(Set carOrders) {
- this.carOrders = carOrders;
- }
-
- public Set getCarOrders() {
- return carOrders;
- }
-
- public void setAlias(Alias alias) {
- this.alias = alias;
- }
-
- public Alias getAlias() {
- return alias;
- }
-
- public void setCars(Set cars) {
- this.cars = cars;
- }
-
- public Set getCars() {
- return cars;
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
Alias.java:
BasicCar.java:
CarOrder.java:
- package basicCar.bean;
-
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
-
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
- import org.hibernate.annotations.GenericGenerator;
-
-
- @Entity
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- @Table(name = "carorder1")
- public class CarOrder implements java.io.Serializable {
- @Id
- @GeneratedValue(generator = "mysqlIncrement")
- @GenericGenerator(name = "mysqlIncrement", strategy = "increment")
- private long cid;
-
- @Column
- private String carName;
-
-
-
- @ManyToOne
- @JoinColumn(name = "salesId")
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- private Salesman salesman;
-
- public CarOrder() {
- }
-
- public CarOrder(String name) {
- this.carName = name;
- }
-
- public long getCid() {
- return this.cid;
- }
-
- public void setCid(long id) {
- this.cid = id;
- }
-
- public String getcarname() {
- return this.carName;
- }
-
- public void setcarname(String cName) {
- this.carName = cName;
- }
-
- public Salesman getSalesman() {
- return this.salesman;
- }
-
- public void setSalesman(Salesman salesman) {
- this.salesman = salesman;
- }
-
-
- }
Address.java:
- package basicCar.bean;
-
- import javax.persistence.Column;
- import javax.persistence.Embeddable;
-
- import org.hibernate.annotations.Cache;
- import org.hibernate.annotations.CacheConcurrencyStrategy;
-
- @SuppressWarnings("serial")
- @Embeddable
- public class Address implements java.io.Serializable {
- @Column
- private String province;
- @Column
- private String city;
- @Column
- private String street;
- @Column
- private String number;
-
- public Address(){}
-
- public Address(String P,String C,String S,String N)
- {
- this.province=P;
- this.city=C;
- this.street=S;
- this.number=N;
- }
-
- public void setProvince(String province) {
- this.province = province;
- }
- public String getProvince() {
- return province;
- }
- public void setCity(String city) {
- this.city = city;
- }
- public String getCity() {
- return city;
- }
- public void setStreet(String street) {
- this.street = street;
- }
- public String getStreet() {
- return street;
- }
- public void setNumber(String number) {
- this.number = number;
- }
- public String getNumber() {
- return number;
- }
-
- }
hibernate.hbm.xml:
- xml version="1.0" encoding="UTF-8"?>
-
- "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driverproperty>
- <property name="hibernate.connection.password">001052property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/chwaproperty>
- <property name="hibernate.connection.username">rootproperty>
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialectproperty>
-
-
- <property name="hibernate.connection.isolation">2property>
-
-
- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider property>
-
-
- <property name="hibernate.generate_statistics">trueproperty>
-
-
- <property name="hibernate.cache.use_second_level_cache">trueproperty>
-
-
- <property name="hibernate.cache.use_query_cache">trueproperty>
-
-
-
- <property name="hibernate.show_sql">trueproperty>
-
- <mapping class="basicCar.bean.Salesman"/>
- <mapping class="basicCar.bean.CarOrder"/>
- <mapping class="basicCar.bean.Alias"/>
- <mapping class="basicCar.bean.BasicCar"/>
- session-factory>
- hibernate-configuration>
ehcache.xml:
- <ehcache>
- <diskStore path="C:\\temp"/>
-
- <defaultCache
- maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- />
-
- <cache name="basicCar.bean.Salesman"
- maxElementsInMemory="5"
- eternal="false"
- timeToIdleSeconds="30"
- timeToLiveSeconds="30"
- overflowToDisk="true"
- />
- <cache name="basicCar.bean.BasicCar"
- maxElementsInMemory="5"
- eternal="false"
- timeToIdleSeconds="30"
- timeToLiveSeconds="30"
- overflowToDisk="true"
- />
-
- <cache name="basicCar.bean.CarOrder"
- maxElementsInMemory="5"
- eternal="false"
- timeToIdleSeconds="30"
- timeToLiveSeconds="30"
- overflowToDisk="true"
- />
-
- <cache name="basicCar.bean.Alias"
- maxElementsInMemory="5"
- eternal="false"
- timeToIdleSeconds="30"
- timeToLiveSeconds="30"
- overflowToDisk="true"
- />
-
- <cache name="sampleCache2"
- maxElementsInMemory="1000"
- eternal="true"
- timeToIdleSeconds="0"
- timeToLiveSeconds="0"
- overflowToDisk="false"
- />
-
-
-
- <cache name="org.hibernate.cache.StandardQueryCache"
- maxElementsInMemory="50"
- eternal="false"
- timeToIdleSeconds="60"
- timeToLiveSeconds="60"
- overflowToDisk="true"
- />
-
-
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
org.hibernate.dialect.DerbyDialect
create
...
com.onjava.modelplanes.domain.PlaneType
com.onjava.modelplanes.domain.ModelPlane
...
(3)
hibernate Annotation标签的使用:
[1]
1.带注释的持久性类也是普通 POJO,它们只是具备了持久性注释的普通 POJO 。
2.事实上,您既可以保持字段的持久性(注释写在成员变量之上),也可以保持属性(注释写在getter方法之上)的持久性。
3.常用的hibernate annotation标签如下:
@Entity --注释声明该类为持久类。将一个Javabean类声明为一个实体的数据库表映射类,最好实现序列化.此时,默认情况下,所有的类属性都为映射到数据表的持久性字段.若在类中,添加另外属性,而非映射来数据库的,要用下面的Transient来注解.
@Table(name=
"promotion_info") --持久性映射的表(表名="promotion_info).@Table是类一级的注解,定义在@Entity下,为实体bean映射表,目录和schema的名字,
默认为实体bean的类名,不带包名.
@Id--注释可以表明哪种属性是该类中的独特标识符(即相当于数据表的主键)。
@GeneratedValue --定义自动增长的主键的生成策略.
@Transient --将忽略这些字段和属性,不用持久化到数据库.适用于,在当前的持久类中,某些属性不是用于映射到数据表,而是用于其它的业务逻辑需要,这时,须将这些属性进行transient的注解.否则系统会因映射不到数据表相应字段而出错.
@Temporal(TemporalType.TIMESTAMP)--声明时间格式
@Enumerated --声明枚举
@Version --声明添加对乐观锁定的支持
@OneToOne --可以建立实体bean之间的一对一的关联
@OneToMany --可以建立实体bean之间的一对多的关联
@ManyToOne --可以建立实体bean之间的多对一的关联
@ManyToMany --可以建立实体bean之间的多对多的关联
@Formula --一个SQL表达式,这种属性是只读的,不在数据库生成属性(可以使用sum、average、max等)
@OrderBy --Many端某个字段排序(List)
1.2
Hibernate 能够出色地自动生成主键。Hibernate/EBJ 3 注释也可以为主键的自动生成提供丰富的支持,允许实现各种策略。
其生成规则由@GeneratedValue设定的.这里的@id和@GeneratedValue都是JPA的标准用法, JPA提供四种标准用法,由@GeneratedValue的源代码可以明显看出.
JPA提供的四种标准用法为TABLE,SEQUENCE,IDENTITY,AUTO.
TABLE:使用一个特定的数据库表格来保存主键。
SEQUENCE:根据底层数据库的序列来生成主键,条件是数据库支持序列。
IDENTITY:主键由数据库自动生成(主要是自动增长型)
AUTO:主键由程序控制。
在指定主键时,如果不指定主键生成策略,默认为AUTO。
@Id
相当于
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
identity:
使用SQL Server 和 MySQL 的自增字段,这个方法不能放到 Oracle 中,Oracle 不支持自增字段,要设定sequence(MySQL 和 SQL Server 中很常用)。
Oracle就要采用sequence了.
同时,也可采用uuid,native等其它策略.(相关用法,上网查询)
[2]
第一个持久性类
@Entity
@Table(name=
"T_MODEL_PLANE")
public
class ModelPlane
implements Serializable {
@Id
@Column(name=
"PLANE_ID")
@GeneratedValue(strategy=GenerationType.AUTO)
//注解于属性中
/*
对于oracle想使用各自的Sequence,设置如下:
@GeneratedValue(strategy = GenerationType.AUTO,generator="PROMOTION_SEQ")
@SequenceGenerator(name="PROMOTION_SEQ",sequenceName="PROMOTION_SEQ")
另外:
对于自动增长后,在数据表中的相应字段,要设置字段为auto_increment.
*/
private Long id;
private String name;
//注解写于getter方法之上.请见下.
//DATE - java.sql.Date
//TIME - java.sql.Time
//TIMESTAMP - java.sql.Timestamp
@Temporal(TemporalType.TIMESTAMP)
@Column(name=
"start_time")
private Date startTime;
//显示0 隐藏1
public
static
enum DisplayType {显示,隐藏}
@Enumerated(value = EnumType.ORDINAL)
//ORDINAL序数
private DisplayType displayType = DisplayType.显示;
//1.sql语句中的字段和表名都应该和数据库相应,而不是类中的字段,
//若带有参数如la.id= id,这个=id才是类中属性
//2.操作字段一定要用别名
@Formula(select COUNT(la.id) from largess la)
private
int count;
//注解于方法中
@Column(name=
"PLANE_ID", length=80, nullable=
true)
//较详细定义
public String getName() {
return name;
}
public
void setName(String name) {
this.name = name;
}
其它的setter,getter省略......
}
该内容将映射到下表中:
CREATE TABLE T_MODEL_PLANE
(
PLANE_ID
long,
PLANE_NAME varchar
其它字段省略...
)
默认情况下,Hibernate 会将持久类以匹配的名称映射到表和字段中。例如,下例中,若不用注解,则会映射到如下一表中:
CREATE TABLE MODELPLANE
(
ID long,
NAME varchar
其它字段省略...
)
[3]
一对多注解:
1.
在一对多注解中,会用到:
"一"方:
@OneToMany --> mappedBy:"多"方的关联属性
(被控方)
"多"方:
@ManyToOne --> @JoinColumn,"多"方定义的外键字段.
如数据表定义外键如下:
FOREIGN KEY (classid) REFERENCES classes(id)
则:
@JoinColumn(name=
"classid")
2.
在双向关联中,有且仅有一端作为主体(owner)端存在:主体端负责维护联接列(即更新),对于不需要维护这种关系的从表则通过mappedNy属性进行声明。mappedBy的值指向另一主体的关联属性。例子中,mappedBy的值为classes。
附加说明:
mappedBy相当于过去的inverse="true".
inverse=false的side(side其实是指inverse=false所位于的class元素)端有责任维护关系,而inverse=true端无须维护这些关系。
3.
cascade与fetch使用说明:
Cascade
CascadeType.PERSIST (级联新建)
CascadeType.REMOVE (级联删除)
CascadeType.REFRESH (级联刷新)
CascadeType.MERGE (级联更新)中选择一个或多个。
CascadeType.ALL
fetch属性:
关联关系获取方式,即是否采用延时加载。
LAZY(默认值)采用延时加载,查询数据时,不一起查询关联对象的数据。而是当访问关联对象时(如:getStudnets()时)才触发相应的查询操作,获取关联对象数据。
EAGER:是在查询数据时,也直接一起获取关联对象的数据。
package oneToMany;
import java.util.Set;
import javax.persistence.*;
/*
注意导入时,是导入:import javax.persistence.*;
非导入org.hibernate的相关类:import org.hibernate.annotations.Entity;
*/
@Entity
@Table(name=
"classes")
public
class Classes
implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private
int id;
private String name;
@OneToMany(cascade=CascadeType.ALL,mappedBy=
"classes")
private Set students;
//getter,setter省略
}
package oneToMany;
import javax.persistence.*;
@Entity
@Table(name=
"student")
public
class Student
implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private
int sid;
private String sname;
//若有多个cascade,可以是:{CascadeType.PERSIST,CascadeType.MERGE}
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name=
"classid")
//student类中对应外键的属性:classid
private Classes classes;
//getter,setter省略
}
public
class TestOneToMany {
/*
CREATE TABLE student ( --要定义外键!!!!!!!
`sid` double NOT NULL auto_increment,
`classid` double NULL,
`sname` varchar(255) NOT NULL,
PRIMARY KEY (sid),
INDEX par_ind (classid),
FOREIGN KEY (classid) REFERENCES classes(id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB
*/
public
static
void main(String[] args)
throws SQLException
{
try
{
SessionFactory sf =
new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
/*
因为mappedBy是定义在classes中,即classes类不负责维护级联关系.即维护者是student.所以,
1.要将clsses的数据,赋给student,即用student的setClasses()方法去捆定class数据;
2.在进行数据插入/更新session.save()/session.update()时,最后操作的是student.
*/
Classes classes=
new Classes();
classes.setName(
"access");
Student st1=
new Student();
st1.setSname(
"jason");
st1.setClasses(classes);
session.save(st1);
Student st2=
new Student();
st2.setSname(
"hwj");
st2.setClasses(classes);
session.save(st2);
tx.commit();
/*
输出如下:
Hibernate: insert into classes (name) values (?)
Hibernate: insert into student (classid, sname) values (?, ?)
Hibernate: insert into student (classid, sname) values (?, ?)
*/
/*
因为一端维护关系另一端不维护关系的原因,我们必须注意避免在应用中用不维护关系的类(class)建立关系,因为这样建立的关系是不会在数据库中存储的。
如上的代码倒过来,则插入时,student的外键值为空.如下:
*/
// Student st1=new Student();
// st1.setSname("jason");
// session.save(st1);
//
// Student st2=new Student();
// st2.setSname("hwj");
// session.save(st2);
//
// Set students=new HashSet();
// students.add(st1);
// students.add(st2);
//
// Classes classes=new Classes();
// classes.setName("access");
// classes.setStudents(students);
// session.save(classes);
/*
输出如下:
Hibernate: insert into student (classid, sname) values (?, ?)
Hibernate: insert into student (classid, sname) values (?, ?)
Hibernate: insert into classes (name) values (?)
*/
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
}
[4]
多对多注解:
在多对多注解中,双方都采用@ManyToMany.
其中被控方,像一对多注解中设置一样,也要设置mappedBy.
其中主控方,不像一对多注解那样,采用@joinColumn,而是采用@joinTable.如下:
@JoinTable(name="j_student_course" ,joinColumns={@JoinColumn(name="sid")},inverseJoinColumns={@JoinColumn(name="cid")})
其中,
如上所说,mappedBy,相当于inverse="true".所以,在@joinTable中的inverseJoinColumns中定义的字段为mappedBy所在类的主键.
joinColumns定义的字段,就是当前类的主键.
@Entity
@Table(name=
"jcourse")
public
class Jcourse {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private
int cid;
private String cname;
@ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.LAZY ,mappedBy=
"courses")
private Set students;
//setter,getter省略....
}
@Entity
@Table(name=
"jstudent")
public
class Jstudent {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private
int sid;
private String sname;
@ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},fetch=FetchType.EAGER)
//inverseJoinColumns中对应的id为以下属性course的对应id.
@JoinTable(name=
"j_student_course" ,joinColumns={@JoinColumn(name=
"sid")},inverseJoinColumns={@JoinColumn(name=
"cid")})
private Set courses;
//setter,getter省略....
}
public
class Test {
public
static
void main(String[] args) {
try
{
SessionFactory sf =
new AnnotationConfiguration().configure().buildSessionFactory();
Session session=sf.openSession();
Transaction tx=session.beginTransaction();
Jcourse course=
new Jcourse();
course.setCname(
"jason-english");
session.save(course);
//先各自保存.
Jcourse course2=
new Jcourse();
course2.setCname(
"herry-english");
session.save(course2);
Set courses=
new HashSet();
courses.add(course);
courses.add(course2);
Jstudent student=
new Jstudent();
student.setSname(
"jason");
student.setCourses(courses);
session.save(student);
// 要用非mapby定义的类(studet)来作为主者(会控制级联关系),一对多,多对一也一样道理.
//可以尝试反过来.
tx.commit();
}
catch(HibernateException e)
{
e.printStackTrace();
}
}
}