浅学Hibernate
来源:互联网 发布:办公邮件软件 编辑:程序博客网 时间:2024/05/16 13:46
Hibernate学习文档
1、配置Hibernate.jar包 有一个src目录下Hibernate.cfg.xml(数据库的配置)
<!DOCTYPEhibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--显示执行的SQL语句-->
<propertyname="show_sql">true</property>
<!--连接字符串-->
<propertyname="connection.url">
jdbc:mysql://localhost:3306/persontest
</property>
<!--连接数据库的用户名-->
<propertyname="connection.username">root</property>
<!--数据库用户密码-->
<propertyname="connection.password">YINFUHAO</property>
<!--数据库驱动-->
<propertyname="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!--选择使用的方言-->
<propertyname="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<propertyname="hbm2ddl.auto">create</property> <!—updateà
<propertyname="show_sql">true</property>
<!--映射文件 -->
<mappingresource="cn/henu/domain/User.hbm.xml"/>
<!—将配置好的xml文件映射到hibernate中-à
</session-factory>
</hibernate-configuration>
一般一个javaBean对应一个xxx.hbm.xml文件 例如User.java
publicclass User {
private Stringname;
privateintid;
private Datebirthday;
public Date getBirthday() {
returnbirthday;
}
publicvoid setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
returnname;
}
publicvoid setName(String name) {
this.name = name;
}
publicint getId() {
returnid;
}
publicvoid setId(int id) {
this.id = id;
}
}
对应的User.hbm.xml
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="cn.henu.domain">
<classname="User"table="person">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<propertyname="birthday"/>
</class>
</hibernate-mapping>
接口UserDao中
publicinterface UserDao {
public User findById(int id);
public User findByName(String name);
publicvoid remove(User user);
publicvoid Update(User user);
publicvoid saveUser(User user);
}
实现类UserDaoImpl中
publicclass UserDaoImplimplements UserDao{
@Override
publicvoid Update(User user) {
Session session=null;
try {
session=HibernateUtil.getSession();
Transaction transaction=session.beginTransaction();
session.update(user);
transaction.commit();
} catch (Exception e) {
if(session!=null){
session.close();
}
}
}
@Override
public User findById(int id) {
Session session=null;
try {
session=HibernateUtil.getSession();
User user=(User)session.get(User.class, id);
return user;
} finally{
if(session!=null){
session.close();
}
}
}
@Override
public User findByName(String name) {
Session session=null;
try {
session=HibernateUtil.getSession();
Criteria criteria=session.createCriteria(User.class);
criteria.add(Restrictions.eq("name",name));
User user=(User) criteria.uniqueResult();
return user;
} finally{
if(session!=null){
session.close();
}
}
}
@Override
publicvoid remove(User user) {
Session session=null;
try {
session=HibernateUtil.getSession();
Transaction transaction=session.beginTransaction();
session.delete(user);
transaction.commit();
} catch (Exception e) {
if(session!=null){
session.close();
}
}
}
@Override
publicvoid saveUser(User user) {
Session session=null;
Transaction transaction=null;
try {
session=HibernateUtil.getSession();
transaction=session.beginTransaction();
session.save(user);
transaction.commit();
} catch (Exception e) {
if(session!=null){
session.close();
}
}
}
}
一般的情况下Hibernator初始化封装
publicfinalclass HibernateUtil {
privatestatic SessionFactorysessionFactory;
private HibernateUtil(){}
static{
Configuration configuration=new Configuration();
configuration.configure();
sessionFactory=configuration.buildSessionFactory();
}
publicstatic SessionFactory getSessionFactory(){
returnsessionFactory;
}
publicstatic Session getSession(){
returnsessionFactory.openSession();
}
}
2、 session中重要方法
delete
update
get 及 load
save
persist
saveOrUpdate
3、 实体对象的三种状态
4、 基本练习user对象的增删改查操作
5、 Hibernate中数据库关系
<1> 一对多关系 一个部门对应多个员工
Department类 Set<Employee> emps; id ; name;
Employee 类 id; name; Department depart;
Department.hbm.xml中
<classname="Department"table="department">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<set name="employees">
<key column="departid"></key>
<one-to-many class="Employee"/>
</set>
</class>
注:若Department类中 List<Employee> emps;
<list-index name="employees">
<key column="departid"></key>
<one-to-many class="Employee"/>
</list> 有顺序之分
<bag></bag> 无顺序之分
Employee.hbm.xml
<classname="Employee"table="employee">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<class>
<2>、多对一 多个职员对应一个部门
Employee.hbm.xml
<classname="Employee"table="employee"discriminator-value="0">
<idname="id">
<generatorclass="native"/>
</id>
<propertyname="name"/>
<many-to-one name="department" column="departid"></many-to-one>
<class>
<3>、一对一关系 Person 对应 IDCard
Person 中 id; name; IDCard;
IDCard 中 Date ; Perosn;
<idname="id">
<generatorclass="foreign"/>
</id>
<propertyname="name"/>
<one-to-one name=”idCard” constrained=”true”/>
<4>、多对多 Teacher Student
Teacher中有Set< Student> students;
<set name=”students” table=”teacher_student”>
<key column=”teacher_id”/>
<many-to-many class=”Student” column=”student_id”/>
</set>
Student中有Set< Teacher> teachers;
<set name=”teachers” table=”teacher_student”>
<key column=”Student_id”/>
<many-to-many class=”Teacher” column=”teacher_id”/>
</set>
6、继承关系的映射
Skiller extends Employee
增加属性 String skill
Sales extends Employee
增加属性 int sell
<1>、同一表中的映射 数据库中生成一个table employee
Employee.hbm.xml 中增加
<classname="Employee"table="employee"discriminator-value="0">
<subclassname="Skiller"discriminator-value="1">
<propertyname="skill"/>
</subclass>
<subclassname="Sales"discriminator-value="2">
<propertyname="sell"/>
</subclass>
<2>、创建table skiller和table sale 和table employee联系
<joined-subclassname="Skiller"table="skilller">
<keycolumn="emp_id"/>
<propertyname="skill"/>
</joined-subclass>
<joined-subclassname="Sales"table="sale">
<keycolumn="emp_id"/> <!—-emp_id自己定义>
<propertyname="sell"/>
</joined-subclass>
<3> emplyee 和 skiller 在一个表中 另建table sale
<subclassname="Skiller"discriminator-value="1">
<propertyname="skill"/>
</subclass>
<subclassname="Sales"discriminator-value="2">
<jointable="sale">
<keycolumn="emp_id"/>
<propertyname="sell"/>
</join>
</subclass>
- 浅学Hibernate
- Quartz浅学
- 浅学javascript
- jQuery 浅学
- 浅学-MVC
- WindowsService 浅学
- 浅学python
- keras浅学
- Nginx浅学
- MySQL浅学
- 浅学CMake
- 浅学CMake
- 浅学spring
- 浅学struts
- 浅学Linux
- 正则浅学
- 浅学Gson
- 浅学vue
- 局域网聊天系统详细设计
- poj2311 Cutting Game-----sg
- 给大家发个Solaris 10_x86补丁下载的地址
- eclipse 中项目->run as->run on server无法在tomcat6下运行,提示The server does not support version 3.0 of the J2E
- ASM元数据备份与恢复:md_backup和md_restore
- 浅学Hibernate
- tomcat7中在项目META-INF中自己写数据库连接池报错解决方法
- ORA-01033: ORACLE initialization or shutdown in progress错误正解
- 静态局部变量
- Ghost系统,无法安装虚拟打印机
- Log4Qt使用笔记(三)
- 多线程和网络编程
- Common Substrings 后缀数组+单调栈
- ORACLE:Health Monitor