hibernate.cfg.xml文件的配置

来源:互联网 发布:围棋摆谱软件 编辑:程序博客网 时间:2024/05/17 22:35

hibernate配置文件

1.hibernate.cfg.xml文件的配置

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/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/hibernate</property>
        <property name="connection.username">root</property>
        <property name="connection.password">bjsxt</property>


        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>


        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>


        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>


        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</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">update</property>


        <mapping resource="com/bjsxt/hibernate/Student.hbm.xml"/>
  <mapping class="com.bjsxt.hibernate.Teacher"/>
    </session-factory>

</hibernate-configuration>


2.Hibernate支持两种配置方式
1).hbm.xml配置:  resource.
2).Annotation的配置:class.

<mapping resource="com/dada/hibernate/Topic.hbm.xml" />
<mapping class="com.dada.hibernate.Category" />

3)为什么他们配置的时候hbm.xml文件使用的是"/"
这种文件路径配置形式,而class使用的是"."呢?
因为hbm.xml文件是需要进行解析的,而文件的加载过程中肯定需要有路径了
而对于类则不一样,因为对于类的处理使用的是反射的方式,它根本不需要知道你的文件
路径,你只需要把类的完整名字交给它它就可以通过反射的形式来获取那个类了


3.hibernate中中manyToOne使用:
1)hbm.xml文件配置方式
<many-to-one name="category" class="Category" fetch="select">
 <column name="catelogid" />
</many-to-one>
2)Annotation的配置方式
@ManyToOne
public Category getCategory() {
 return category;
}

在使用hibernate的Annotation配置方式进行配置的时候,如果使用了manyToOne的话一定要在数据库中把它们的主外键关系给设置好,否则在你向程序添加数据的时候就会出错,因为在hibernate看来,具有manyToOne的两个实体之间必然是具有外间关系的两个实体.

如果你不去设置的话,那么不好意思hibernate会自动的帮助你添加的,但即便如此也还是会出错的,因为他帮你添加的那个列是以在实体中所配置的那个属性加上"_id"生成的,在你向数据库添加数据的时候呢数据库中还没有出现这个列因此你不可能为它赋值,但是如果不赋值的话,程序又会出错的.

4.指定实体对应数据库中的表的名字(默认表的名字跟类的名字第一个字母小写之后相同,不同需要指定)
1)hbm.xml
<class name="Category" table="category" catalog="dandan">

2)Annotation
@Entity
@Table(name = "_teacher")

5.指定表中的主键生成策略
1)hbm.xml
(1)唯一主键
唯一主键中的identity生产方式:
这种方式采用数据库提供的主键生成机制。如DB2、SQL Server、MySQL中的主键生成机制。
.hbm.xml
 <id name="id" type="java.lang.Integer">
 <column name="id" />
 <generator class="identity" />
</id>
唯一主键中的squence生成方式:
<id name="userId" type="java.lang.Long">
 <column name="USER_ID" precision="9" scale="0" />
 <generator class="sequence">
 <param name="sequence">PUB_ID_SEQ</param>
 </generator>
</id>

 

(2)联合主键,这种情况下联合主键里面所包含的列需要重新的生成一个联合主键类
<composite-id name="id" class="com.dada.hibernate.TwoId">
 <key-property name="id" type="java.lang.Integer">
  <column name="id" />
 </key-property>
 <key-property name="name" type="java.lang.String">
  <column name="name" length="20" />
 </key-property>
</composite-id>

2)Annotation
(1)唯一主键

唯一主键中的identity生产方式:
@Id
@GeneratedValue
public int getId() {
 return id;
}

唯一主键中的squence生成方式:
@Id
@SequenceGenerator(name="sequenceGenerator",sequenceName="ACTIVITIESSCOPE_SEQ")
@GeneratedValue(generator="sequenceGenerator",strategy=GenerationType.SEQUENCE)
@Column(name = "ID", unique = true, nullable = false, precision = 10, scale = 0)
public Long getId() {
   return this.id;
}

注意:@GeneratedValue中的generator不是sequence的名字,而是@SequenceGenerator中的name的名字;@SequenceGenerator中的sequenceName才是定义sequence的名字。

唯一主键中的table生成方式:
在实体类的class定义上配置如下
@Entity
@javax.persistence.TableGenerator(
 //指定的是这个主键生成策略在当前类里面访问的时候所使用的名字
 name="Teacher_GEN",
 //指定这个主键生成表在数据库中的名字
 table="GENERATOR_TABLE",
 //指定这个表里面生成的值的名字所对应的列
 pkColumnName = "pk_key",
 //指定这个表里面生成的值的列的名字
 valueColumnName = "pk_value",
 //指定当前表使用的那个行的列的名字
 pkColumnValue="Teacher",
 //指定分配的大小
 allocationSize=1
)

在id属性上面配置如下
@Id
 @GeneratedValue(strategy=GenerationType.TABLE, generator="Teacher_GEN")
 public int getId() {
  return id;
 }

(2)联合主键
在类上面标注@IdClass(TeacherPK.class)指定的是这个类所使用的联合主键是哪一个类
在类的联合主键的id上标注它,表面这个是个联合主键
@EmbeddedId
public TeacherPK getPk() {
 return pk;
}

在作为联合主键的类上面标注
@Embeddable表明这个类是作为联合主键来使用的

备注:如果使用的数据库是mysql数据库,那么需要注意一个问题是,在使用联合主键的时候,mysql是不允许里面存在着自动增长的列的,也就是说,如果一个列是自动增长的那么这个列就应该被当做是唯一的主键,如果要使用联合主键的话,那么这些作为联合主键的列里面是不可以有自动增长的列存在的,否则就会报错的.

6.设置某个属性是enum类型的变量

1)hbm.xml
enum在使用的时候因为数据库里面没有对应的数据类型,所以在存储的时候还是要转换为其他类型所以如果在hbm.xml文件里面使用enum的话基本上就是把对应的enum类型转换为String类型之后再保存了.

2)Annotation
@Enumerated(EnumType.STRING)
public Gender getGender() {
 return gender;
}

7.SessionFactory获得方式和使用
1)hbm.xml
SessionFactory sf = new Configuration().configure().buildSessionFactory()
2)Annoation
SessionFactory sf = new AnnotationConfiguration().configure().buildSessionFactory();

8.@Temporal

1)日期:@Temporal用法

@Temporal(TemporalType.DATE)
 @Column(name = "applyDate", nullable = false, length = 10)
 public Date getApplyDate() {
  return applyDate;
 }

在页面端取值:2011-04-12

2)时间:

@Temporal(TemporalType.TIME)

在页面端取值:22:50:30

3)日期和时间(默认):

@Temporal(TemporalType.TIMESTAMP)
在页面端取值:2011-04-12 22:51:34.0


9.为boolean类型的值赋值
如果一个属性是boolean类型的,那么这个属性就需要在hbm.xml文件中配置的时候使用boolean类型
,在数据库mysql中使用的是tinyint类型的,只需要给它长度为1就好了
<property name="good" type="boolean"/>

 

 

 

 

原创粉丝点击