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各个行的的价格总和作为一个字段,在表中其实不存在,但是可以以对象的字段存在
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。 |
相关文章推荐
- 史上最简单的Hibernate入门简介 doodoofish [原作]
- 史上最简单的Hibernate入门简介
- JBuilder+Hibernate+Tomcat快速入门
- Hibernate入门
- Hibernate入门03 - 配置文件
- Java开源项目Hibernate快速入门
- Hibernate 的原理与配置快速入门
- [转贴] Hibernate入门 - 基础配置
- [转贴] hibernate入门 - Transaction
- 史上最简单的Hibernate入门简介
- Hibernate 入门(原创)
- Hibernate入门之小例子(转载)
- 史上最简单的Hibernate入门简介 (转)
- Hibernate快速入门
- 史上最简单的Hibernate入门简介
- Hibernate 入门(原创)
- Hibernate入门经典
- Hibernate 的原理与配置快速入门
- Hibernate入门02 - 第一个Hibernate程序
- Hibernate入门04 - 提供JDBC连接