您的位置:首页 > 其它

hibernate入门

2014-07-15 22:18 204 查看
需要的jar包

package cn.itcast.fomula;

/**

* 表对应的javaBean,该javaBean的数据最终要存入到数据库中

* 实体域对象

*/

public class Customer {

private Integer id;

private String name;

private Integer age;

private String des;

private Double price;

//通过formula属性赋值

private Double totalPrice;//在数据局中没有对应的字段

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public String getDes() {

return des;

}

public void setDes(String des) {

this.des = des;

}

public Double getPrice() {

return price;

}

public void setPrice(Double price) {

this.price = price;

}

public Double getTotalPrice() {

return totalPrice;

}

public void setTotalPrice(Double totalPrice) {

this.totalPrice = totalPrice;

}

}

映射文件:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="cn.itcast.fomula.Customer" table="customers">

<id name="id" type="integer">

<column name="id"/>

<generator class="increment"/>

</id>

<property name="name" type="string">

<column name="name"/>

</property>

<property name="age" type="integer">

<column name="age"/>

</property>

<property name="des" type="string">

<column name="des"/>

</property>

<property name="price" type="double">

<column name="price"/>

</property>

<!-- 该属性在表中没有对应的记录 -->

<property name="totalPrice" type="double" formula="(select sum(c.price) from customers c)">

</property>

</class>

</hibernate-mapping>

使用表中所有customers各个行的的价格总和作为一个字段,在表中其实不存在,但是可以以对象的字段存在

映射属性

作用

<property>

insert属性

若为false,在insert语句中不包含该字段,该字段永远不能被插入。默认值true。

<property>

update属性

若为false,update语句不包含该字段,该字段永远不能被更新。默认值为true。

<class>

mutable属性

若为false,等价于所有的<property>元素的update属性为false,整个实例不能被更新。默认为true。

<class>

dynamic-insert属性

若为true,等价于所有的<property>元素的insert为true,保存一个对象时,动态生成insert语句,语句中仅包含取值不为null的字段。默认false。 这和在property中insert的区别在于,前者正对一个字段。后者针对的是整个类

<class>

dynamic-update属性

若为true,等价于所有的<property>元素的update为true,更新一个对象时,动态生成update语句,语句中仅包含取值不为null的字段。默认false。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: