1、实体bean的配置
(1)用配置文件和bean来配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>
classpath:com/.../bean
</value>
</list>
</property>
</bean>
(2)无专门bean配置文件的配置(需要: Hibernate库文件,Hibernate Annotations库,ejb3-persstence.jar(Java 持久化API))
<!--Hibernate SessionFatory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<!--添加bean -->
<value>....bean.AddressBook</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
2、Hibernate Annotations详解
参阅:[url]http://weishuwei.iteye.com/blog/95581[/url]
需要引用的包
import javax.persistence.*;
import org.hibernate.annotations.*;
映射实体类
@Entity
@Table(name = "HRMS_EMPINFO_COMPANY")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@SequenceGenerator(name = "SEQ_STORE", sequenceName = "HRMS_EMPLOYEEINFO_SEQUENCE", allocationSize = 1)
public class Company implements java.io.Serializable
映射标识符
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
public Long getId() {
return id;
}
映射属性
@Column(name = "REMARKS", length = 200)
public String getRemarks() {
return remarks;
}
@Column(name = "POSITION", length = 20,updatable=false)
public String getPosition() {
return position;
}
关联关系之一对一
@OneToOne(mappedBy = "address")
public User getUser() {
return user;
}
//a、两边都定义了@OneToOne,但都没有定义mappedBy,则user和address表都会生成到对方的外键,双方都是这个关系的拥有者。
//b、两边都定义了@OneToOne,如果user定义了mappedBy,则在address表生成到user的外键,address是这个关系的拥有者;如果address定义
//了mappedBy,则在user表生成到address的外键,user是这个关系的拥有者。
关联关系之一对多
@OneToMany(targetEntity = Employee.class, cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "company")
@JoinColumn(name = "COMPANY_ID")
public List<Employee> getEmployees() {
return employees;
}
//company为Employee.class实体中的Company.class类型的属性名。表明Company.class是关系的拥有者。
//其中定义mappedBy的是@OneToMany,也就是说One这一方是关系的拥有者。Many一方的表中生成到关联类的外键。
关联关系之多对一
@ManyToOne(targetEntity = Company.class, fetch = FetchType.LAZY)
@JoinColumn(name = "COMPANY_ID")
public Company getCompany() {
return company;
}
@ManyToOne(targetEntity = Job.class, fetch = FetchType.LAZY)
@JoinColumn(name = "JOB",updatable=false)
public Job getJob() {
return job;
}
3、遇到问题
(1)、删除某个实体时,会出现更新语句。如
Hibernate: update HRMS_EMPINFO_EMPLOYEE set TEAM_ID=null where TEAM_ID=?
Hibernate: delete from HRMS_EMPINFO_EMPLOYEE where id=?
Hibernate: delete from HRMS_EMPINFO_TEAM where id=?
分析:hibernate配置文件问题
解决:hibernate配置文件中给一对多关系增加主操作配置参数,这样只会更新一次(更新操作),只有删除(删除操作)。
Company实体bean的配置
@OneToMany(targetEntity = Employee.class, cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "company")
@JoinColumn(name = "COMPANY_ID")
Employee实体bean的配置
@ManyToOne(targetEntity = Company.class, fetch = FetchType.LAZY)
@JoinColumn(name = "COMPANY_ID")
public Company getCompany() {
return company;
}
(2)、org.hibernate.NonUniqueObjectException异常
[org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/employeeManage].[default]] - <Servlet.service() for servlet default threw exception>
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [....bean.Interview#230]
解决办法:使用meger方法代替update方法
(3)、在hibernate中使用SQL语句示例
String sqlStart = "select app.* ";
String sql = "from HRMS_RECRUITMENT_APPLIER app,"
+ "HRMS_RECRUITMENT_JOB job where app.APPLY_JOB=job.id "
+ "and job.status=? and app.ID in (select USER_ID from "
+ "(select a.* from HRMS_RECRUITMENT_INTERVIEW a ,"
+ "(select USER_ID,max(INTERVIEWDATE) mt from "
+ "HRMS_RECRUITMENT_INTERVIEW group by USER_ID) b "
+ "where a.USER_ID=b.USER_ID and a.INTERVIEWDATE=b.mt) temp "
+ "where interviewstatus=?) ";
String sqlEnd = " order by app.id";
Query query = this.openSession()
.createSQLQuery(sqlStart + sql + sqlEnd).addEntity("app",
Applier.class);
query.setInteger(0, jobStatus);
query.setLong(1, interviewStatus);
int startIndex = Page.getStartOfPage(pageNo, pageSize);
List<Applier> list = query.setFirstResult(startIndex).setMaxResults(
pageSize).list();
(4)、模糊查询
criteria.add(Restrictions.like(propNames[i], propValues[i], MatchMode.ANYWHERE));
Hibernate相关知识参阅: [url]http://aumy2008.blogbus.com/[/url]
(1)用配置文件和bean来配置
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>
classpath:com/.../bean
</value>
</list>
</property>
</bean>
(2)无专门bean配置文件的配置(需要: Hibernate库文件,Hibernate Annotations库,ejb3-persstence.jar(Java 持久化API))
<!--Hibernate SessionFatory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<!--添加bean -->
<value>....bean.AddressBook</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.generate_statistics">false</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
<prop key="hibernate.autoReconnect">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
</props>
</property>
</bean>
2、Hibernate Annotations详解
参阅:[url]http://weishuwei.iteye.com/blog/95581[/url]
需要引用的包
import javax.persistence.*;
import org.hibernate.annotations.*;
映射实体类
@Entity
@Table(name = "HRMS_EMPINFO_COMPANY")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@SequenceGenerator(name = "SEQ_STORE", sequenceName = "HRMS_EMPLOYEEINFO_SEQUENCE", allocationSize = 1)
public class Company implements java.io.Serializable
映射标识符
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_STORE")
public Long getId() {
return id;
}
映射属性
@Column(name = "REMARKS", length = 200)
public String getRemarks() {
return remarks;
}
@Column(name = "POSITION", length = 20,updatable=false)
public String getPosition() {
return position;
}
关联关系之一对一
@OneToOne(mappedBy = "address")
public User getUser() {
return user;
}
//a、两边都定义了@OneToOne,但都没有定义mappedBy,则user和address表都会生成到对方的外键,双方都是这个关系的拥有者。
//b、两边都定义了@OneToOne,如果user定义了mappedBy,则在address表生成到user的外键,address是这个关系的拥有者;如果address定义
//了mappedBy,则在user表生成到address的外键,user是这个关系的拥有者。
关联关系之一对多
@OneToMany(targetEntity = Employee.class, cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "company")
@JoinColumn(name = "COMPANY_ID")
public List<Employee> getEmployees() {
return employees;
}
//company为Employee.class实体中的Company.class类型的属性名。表明Company.class是关系的拥有者。
//其中定义mappedBy的是@OneToMany,也就是说One这一方是关系的拥有者。Many一方的表中生成到关联类的外键。
关联关系之多对一
@ManyToOne(targetEntity = Company.class, fetch = FetchType.LAZY)
@JoinColumn(name = "COMPANY_ID")
public Company getCompany() {
return company;
}
@ManyToOne(targetEntity = Job.class, fetch = FetchType.LAZY)
@JoinColumn(name = "JOB",updatable=false)
public Job getJob() {
return job;
}
3、遇到问题
(1)、删除某个实体时,会出现更新语句。如
Hibernate: update HRMS_EMPINFO_EMPLOYEE set TEAM_ID=null where TEAM_ID=?
Hibernate: delete from HRMS_EMPINFO_EMPLOYEE where id=?
Hibernate: delete from HRMS_EMPINFO_TEAM where id=?
分析:hibernate配置文件问题
解决:hibernate配置文件中给一对多关系增加主操作配置参数,这样只会更新一次(更新操作),只有删除(删除操作)。
Company实体bean的配置
@OneToMany(targetEntity = Employee.class, cascade = CascadeType.REMOVE, fetch = FetchType.LAZY, mappedBy = "company")
@JoinColumn(name = "COMPANY_ID")
Employee实体bean的配置
@ManyToOne(targetEntity = Company.class, fetch = FetchType.LAZY)
@JoinColumn(name = "COMPANY_ID")
public Company getCompany() {
return company;
}
(2)、org.hibernate.NonUniqueObjectException异常
[org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/employeeManage].[default]] - <Servlet.service() for servlet default threw exception>
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [....bean.Interview#230]
解决办法:使用meger方法代替update方法
(3)、在hibernate中使用SQL语句示例
String sqlStart = "select app.* ";
String sql = "from HRMS_RECRUITMENT_APPLIER app,"
+ "HRMS_RECRUITMENT_JOB job where app.APPLY_JOB=job.id "
+ "and job.status=? and app.ID in (select USER_ID from "
+ "(select a.* from HRMS_RECRUITMENT_INTERVIEW a ,"
+ "(select USER_ID,max(INTERVIEWDATE) mt from "
+ "HRMS_RECRUITMENT_INTERVIEW group by USER_ID) b "
+ "where a.USER_ID=b.USER_ID and a.INTERVIEWDATE=b.mt) temp "
+ "where interviewstatus=?) ";
String sqlEnd = " order by app.id";
Query query = this.openSession()
.createSQLQuery(sqlStart + sql + sqlEnd).addEntity("app",
Applier.class);
query.setInteger(0, jobStatus);
query.setLong(1, interviewStatus);
int startIndex = Page.getStartOfPage(pageNo, pageSize);
List<Applier> list = query.setFirstResult(startIndex).setMaxResults(
pageSize).list();
(4)、模糊查询
criteria.add(Restrictions.like(propNames[i], propValues[i], MatchMode.ANYWHERE));
Hibernate相关知识参阅: [url]http://aumy2008.blogbus.com/[/url]