SUPER.M A M

L Y

导航

Hibernate hbm映射文件的详解

错误演示

第一步:导入jar包   省略

第二部:创建Person类

 

package cn.hibernate.bean;

 

import java.util.Date;

 

public class Person {

 

private Integer pId;
private String pName;
private int age;
private Date brithdayDate;
private boolean gender;
private byte[] photo;
private String desc; //描述




public Integer getpId() {
return pId;
}
public void setpId(Integer pId) {
this.pId = pId;
}

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getpName() {
return pName;
}
public void setpName(String pName) {
this.pName = pName;
}
public Date getBrithdayDate() {
return brithdayDate;
}
public void setBrithdayDate(Date brithdayDate) {
this.brithdayDate = brithdayDate;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public byte[] getPhoto() {
return photo;
}
public void setPhoto(byte[] photo) {
this.photo = photo;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}

 

第三步:创建Person的映射文件

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.hibernate.bean">
<!--
<class>标签:配置对象与表 之间关系
*name:默认情况,确定对象全限定类名
如果在<hibernate-mapping>配置package,确定javabean所在的包,可以只简单类名
*table:数据库的表名
*catalog:使用的数据库名称,默认值:hibernate.cfg.xml配置url设置的数据库名称
url # jdbc:mysql://localhost:3306/minemysql
<id>标签配置主键,要求每一个表都应该存在主键
* name :确定javabean那个属性作为主键
<property>标签 配置javabean的普通属性
* name: 确定属性名称
* type: 确定属性类型
java类型:type="java.lang.String"
hibernate类型:type="string"
timestamp:时间戳,数据更改,当前字段内容将自动使用系统时间
time:时间
date:日期
binary:二进制,大数据类型
数据库类型: 使用子标签<column>
name:列名
sql-type:数据库类型。例如:varchar(50)
* column: 数据库的列名:默认值与name的取值相同
* length: 确定列的大小
* unique:唯一(添加唯一约束)
* not-null:不为空
-->
<class name="Person" table="t_person">
<id name="pId">
<!-- 主键生成策略:固定值 -->
<generator class="native"></generator>
</id>
<property name="pName" >
<column name="pName" sql-type="varchar(50)"></column>
</property>
<property name="age" type="integer" column="age"></property>
<property name="brithdayDate" type="date"></property>
<property name="gender" type="boolean"></property>
<property name="photo" type="binary" not-null="true" unique="false" length="350000"></property>
<property name="desc" column="`desc`"></property>
</class>
</hibernate-mapping>

第四步:创建核心配置文件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>
<!-- #1 基本的四项
property.name 取值如果以‘hibernate’开头,可以省略
以“hibernate.connection.driver_class”与“connection.driver_class”相同
-->
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/minemysql
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>

<!-- #2方言 -->
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>


<!-- #3 sql -->
<!-- 是否显示sql语句 -->
<property name="hibernate.show_sql">true</property>
<!-- 是否格式化sql语句 不格式化显示一行 格式化显示多行 -->
<property name="hibernate.format_sql">true</property>
<!-- 是否显示注释,提供当前sql语句操作对象 -->
<property name="hibernate.use_sql_comments">true</property>


<!-- #4如何创建表(不重要)
create:每一次加载cfg.xml文件都将创建表,程序关闭时,表不进行删除 [初始化,测试时使用]
如果表存在则先删除后创建
create-drop:每一次加载cfg.xml文件都将创建表,程序关闭时,表进行删除
必须执行factory.close()才能删除
update:如果表不存在则创建,如果表存在,先回检查*.hbm.xml文件是否和表匹配,
如果不匹配将更新表结构(只添加,不删除)
validate:加载cfg.xml进效验,映射文件和数据表是否匹配,如果匹配正常操作,如果不匹配则抛出异常
### 显示的开发中先有的表,再有的映射文件
* 表 由DBA创建
-->
<property name="hibernate.hbm2ddl.auto">update</property>


<!-- #5取消bean效验 -->
//省略下面会给讲解什么时候需要配置<property name="javax.persistence.validation.mode">none</property>

</session-factory>
</hibernate-configuration>

 

第五步:创建测试类

 

package cn.hibernate.test;

 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

 

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;

 

import cn.hibernate.bean.Person;

 

public class TestApp {

 

@Test
public void demo01() throws IOException{
FileInputStream is = new FileInputStream(new File("1.jpg"));
byte[] photo = new byte[is.available()];
is.read(photo);
is.close();

Person son = new Person();
son.setpName("杰克1");
son.setAge(19);
son.setBrithdayDate(new Date());
son.setGender(true);
son.setPhoto(photo);
son.setDesc("这个家伙有头像");





Configuration configuration = new Configuration().configure();
//采用手动方式添加
configuration.addClass(Person.class);

SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();

session.save(son);

transaction.commit();
session.close();
sessionFactory.close();


}
}

 

 

 

 

 

 

测试时出现的错误如下解决

如果出现图片太大可以在映射文件中来修改所能保存图片的大小

 

出现:org.hibernate.HibernateException: Unable to get the default Bean Validation factory     错误

分析:在javaweb6.0项目中添加一个jar 

解决:hibernate.cfg.xml中配置取消  

 

特殊字符

分析:sql数据不能使用关键字(desc,order等)

解决方法:在列名的左右添加重音符 ("``")

 

posted on 2017-06-03 14:17  super.  阅读(4253)  评论(0编辑  收藏  举报