Hibernate逍遥游记-第3章对象-关系映射基础-access="field"、dynamic-insert、dynamic-update、formula、update=false
1.
package mypack; import java.util.*; public class Monkey{ private Long id;
private String firstname;
private String lastname;
private char gender;
private int age;
private int avgAge;
private String description; public Monkey() {} public Monkey(String firstname,String lastname,char gender,int age,String description){
this.firstname=firstname;
this.lastname=lastname;
this.gender=gender;
this.age=age;
this.description=description;
} public Long getId() {
return this.id;
} private void setId(Long id) {
this.id = id;
} public String getFirstname(){
return firstname;
} public void setFirstname(String firstname){
this.firstname=firstname;
} public String getLastname(){
return lastname;
} public void setLastname(String lastname){
this.lastname = lastname;
} public String getName(){
return firstname+ " "+lastname;
} public void setName(String name){
StringTokenizer t=new StringTokenizer(name);
firstname=t.nextToken();
lastname=t.nextToken();
} public int getAge(){
return this.age;
} public void setAge( int age ){
this.age = age;
} public int getAvgAge(){
return this.avgAge;
} private void setAvgAge( int avgAge){
this.avgAge = avgAge;
} public char getGender(){
return this.gender;
} public void setGender(char gender){
if(gender!='F' && gender!='M'){
throw new IllegalArgumentException("Invalid Gender");
}
this.gender =gender ;
} public String getDescription(){
return this.description;
} public void setDescription(String description){
this.description=description;
}
}
2.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="mypack.Monkey" table="MONKEYS" dynamic-insert="true" dynamic-update="true" >
<id name="id">
<generator class="increment"/>
</id> <property name="name" column="NAME" />
<property name="gender" column="GENDER" access="field" />
<property name="age" column="AGE" /> <property name="avgAge"
formula="(select avg(m.AGE) from MONKEYS m)" /> <property name="description" type="text" column="`MONKEY DESCRIPTION`"/> </class>
</hibernate-mapping>
3.
package mypack; import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory;
static{
try{
Configuration config = new Configuration()
.configure(); //加载hibernate.cfg.xml文件中配置的信息
sessionFactory = config.buildSessionFactory();
}catch(RuntimeException e){e.printStackTrace();throw e;}
} public Monkey loadMonkey(long monkey_id){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,new Long(monkey_id));
tx.commit();
return monkey;
}catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void saveMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void loadAndUpdateMonkey(long monkeyId) {
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
Monkey monkey=(Monkey)session.get(Monkey.class,new Long(monkeyId));
monkey.setDescription("勇敢无畏!");
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void updateMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.update(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void printMonkey(Monkey monkey){
System.out.println("name:"+monkey.getName());
System.out.println("gender:"+monkey.getGender());
System.out.println("description:"+monkey.getDescription());
System.out.println("age:"+monkey.getAge());
System.out.println("avgAge:"+monkey.getAvgAge());
} public void test(){
Monkey monkey=new Monkey("悟空","孙",'M',500,"神通广大!");
saveMonkey(monkey); monkey=loadMonkey(1);
printMonkey(monkey); monkey.setDescription("齐天大圣!");
updateMonkey(monkey);
printMonkey(monkey); loadAndUpdateMonkey(1);
printMonkey(monkey);
} public static void main(String args[]) {
new BusinessService().test();
sessionFactory.close();
}
}
4.
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/sampledb
</property>
<property name="connection.username">
root
</property>
<property name="connection.password">
1234
</property>
<property name="show_sql">true</property>
<mapping resource="mypack/Monkey.hbm.xml" />
</session-factory>
</hibernate-configuration>
5.
drop database if exists SAMPLEDB;
create database SAMPLEDB;
use SAMPLEDB; create table MONKEYS (
ID bigint not null,
NAME varchar(15),
GENDER char(1),
AGE int,
`MONKEY DESCRIPTION` text,
primary key (id)
);
Hibernate逍遥游记-第3章对象-关系映射基础-access="field"、dynamic-insert、dynamic-update、formula、update=false的更多相关文章
- Hibernate逍遥游记-第2章-使用hibernate.properties
1. package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util. ...
- Hibernate逍遥游记-第10章 映射继承关系-003继承关系树中的每个类对应一个表(joined-subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-002继承关系树中的根类对应一个表(discriminator、subclass)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第8章 映射组成关系(<component>、<parent>)
一. 1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)
1. 2. 3. 4. 5. 6. 7.
- Hibernate逍遥游记-第1章-JDBC访问数据库
1. package mypack; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.sw ...
- Hibernate逍遥游记-第15章处理并发问题-003乐观锁
1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...
- Hibernate逍遥游记-第15章处理并发问题-002悲观锁
1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...
随机推荐
- 一款jquery编写图文下拉二级导航菜单特效
一款jquery编写图文下拉二级导航菜单特效,效果非常简洁大气,很不错的一款jquery导航菜单特效. 这款jquery特效适用于很多的个人和门户网站. 适用浏览器:IE8.360.FireFox.C ...
- php中echo、print、print_r、printf的返回值
1.echo 无返回值,是一个语言结构.在输出多个参数时不可以使用小括号; 2.print返回值为1:如:$x = 0; echo print $x."<br/>";/ ...
- UDP HelloWord
Client.cpp #include <stdio.h> #include <winsock2.h> #pragma comment (lib,"ws2_32&qu ...
- Delphi XE5教程4:程序和单元概述
内容源自Delphi XE5 UPDATE 2官方帮助<Delphi Reference>,本人水平有限,欢迎各位高人修正相关错误!也欢迎各位加入到Delphi学习资料汉化中来,有兴趣者可 ...
- WPF 心形线算法
今天在网上查找下心形算法公式,自己便按照公式写下来标记在博客,主要是方便以后查看! private int maxStep = 520; private double radius; private ...
- CentOS服务器Http压力测试之ab
ab的全称是Apache Bench,是Apache自带的网络压力测试工具,相比于LR.JMeter,是我所知道的 Http 压力测试工具中最简单.最通用的. ab命令对发出负载的计算机要求很低,不会 ...
- DB天气app冲刺二阶段第九天
今天是第九天了 不管怎么样也要收尾了赶紧,毕竟不可能做到尽善尽美了,时间不够了所以要把该砍掉的砍点,然后应对下周的大二同学的面试.尽量做好界面的美化工作这是最基本的了.毕竟我一直崇尚的就是UI设计了. ...
- Quartz 2D画虚线-b
这里使用的函数为 CGContextSetLineDash,有四个参数 CGContextSetLineDash(<#CGContextRef _Nullable c#>, < ...
- Careercup - Facebook面试题 - 4892713614835712
2014-05-02 09:54 题目链接 原题: You have two numbers decomposed in binary representation, write a function ...
- 【转】jquery-取消冒泡
转自:http://blog.163.com/css_mm/blog/static/209182176201262665157634/ 1.通过返回false来取消默认的行为并阻止事件起泡. jQue ...