- 浏览: 143089 次
- 性别:
- 来自: 浙江
文章分类
最新评论
-
bnmnba:
bnmnba 写道bnmnba 写道 三个文件的学习学习。这么 ...
linux下C/C++ 入门hello world(转) -
bnmnba:
bnmnba 写道 三个文件的学习学习。这么写:#includ ...
linux下C/C++ 入门hello world(转) -
bnmnba:
三个文件的学习学习。
linux下C/C++ 入门hello world(转) -
fcmfcm01:
个人觉得没什么必要,控制访问权限完全可以由方法来实现,就像ja ...
C#中属性和字段的区别和联系<转载> -
eagledame:
很详细 谢谢了~
JAVA IO流的老师小结
第一天 ********************************************************** 原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Sesssion 4.创建事务Transation 5.持久化操作 6.提交事务 7.关闭Session 8.关闭SesstionFactory 第一步 写po类 package hibernate; //biz public class Account { private Long oid; private String actNo; private double bal; public Account() { } public Account(String actNo, double bal) { super(); this.actNo = actNo; this.bal = bal; } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public double getBal() { return bal; } public void setBal(double bal) { this.bal = bal; } } 第二步 注意2个XML文件的位置 Account.hbm.xml <?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 package="hibernate"> <class name="Account" table="t_act"> <id name="oid" column="OID"> <generator class="hilo">//算法的类名 <param name="table">t_hilo</param> <param name="column">HI</param> </generator> </id> <property name="actNo" column="ACTNO"/> <property name="bal" column="BALANCE"/> </class> </hibernate-mapping> hibernate.cfg.xml <?xml version="1.0"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@192.168.7.88:1521:tarena</property> <property name="connection.username">rensx</property> <property name="connection.password">rensx</property> <property name="connection.isolation">2</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="myeclipse.connection.profile">rensx_oracle</property> <mapping resource="hibernate/Account.hbm.xml" /> </session-factory> </hibernate-configuration> 第三步 create table t_act( OID number(12) primary key, ACTNO varchar(30) not null unique, BALANCE number(12,2) not null); drop table t_hilo; create table t_hilo( HI number(12) primary key); insert into t_hilo values(1); commit; 第四步 import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] args) { Configuration cfg = new Configuration().configure();//创建Configuration,初始化实例的所有变量,加载hibernate.cfg.xml至该实例 //通过hibernate.cfg.xml中的mapping节点配置并加载.hbm.xml文件 SessionFactory sf = cfg.buildSessionFactory();//创建SessionFactory实例 Account act = new Account("act_001", 2000.0); Session s = sf.openSession();//创建连接 // 非捕捉异常discriminator-value try { s.beginTransaction();//session创建事务实例 s.save(act);//对数据库的访问操作 s.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); sf.close(); } } } ************************************************************************************** 第二天 SessionFactory:重量级别的 线程安全的 开销比较大 Session:轻量级的 线程不安全 工具类 public class HibernateUtil { private static SessionFactory sf; public static SessionFactory getSessionFactory() { if (sf == null || sf.isClosed()) { try { sf = new Configuration().configure().buildSessionFactory(); } catch (HibernateException e) { e.printStackTrace(); } } return sf; } public static void release() { if(sf!=null&&!sf.isClosed()){ sf.close(); } } } public class Test2 { public static void main(String[] args) { Session s = null; try { s = HibernateUtil.getSessionFactory().openSession(); s.beginTransaction(); String hql = "from TestRen a where a.name=?";// 这是类 不是表 // 当具有唯一性 返回一个对象时 不用list集合了 // List l=s.createQuery(hql).setString(0, "ren").list(); Query q = s.createQuery(hql); q.setString(0, "ren");// 参数从0开始 List l = q.list(); StringBuffer sb = new StringBuffer(); for (TestRen a : (List<TestRen>) l) { sb.append(a.getOid() + " " + a.getName() + "\n"); } System.out.println(sb.toString()); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); HibernateUtil.release(); } } } public class Test3 { public static void main(String[] args) { Session s = null; try { s = HibernateUtil.getSessionFactory().openSession(); s.beginTransaction(); // String hql="from TestRen a where a.name=?";//这是类 不是表 String hql = "from TestRen a where a.name=:canshu";// 可以不用占位符号 使用参数 // 当具有唯一性 返回一个对象时 不用list集合了 TestRen a = (TestRen) s.createQuery(hql).setString("canshu", "ren") .uniqueResult(); StringBuffer sb = new StringBuffer(); sb.append(a.getOid() + " " + a.getName() + "\n"); System.out.println(sb.toString()); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); HibernateUtil.release(); } } } public class Test4-1{ public static void main(String[] args) { Session s = null; try { s = HibernateUtil.getSessionFactory().openSession(); s.beginTransaction(); //List params =new Arra String hql = "from TestRen a where a.name like ?"; Query q = s.createQuery(hql); q.setString(0, "ren%");// 参数从0开始 List l = q.list(); StringBuffer sb = new StringBuffer(); for (TestRen a : (List<TestRen>) l) { sb.append(a.getOid() + " " + a.getName() + "\n"); } System.out.println(sb.toString()); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); HibernateUtil.release(); } } } public class Test4-2{ public static void main(String[] args) { Session s = null; try { s = HibernateUtil.getSessionFactory().openSession(); s.beginTransaction(); List params =new ArrayList(); params.add("ren"); params.add("ren2"); params.add("ren3"); String hql = "from TestRen a where a.name in(:can)"; Query q = s.createQuery(hql); q.setParameterList("can", params);//最好这样用 List l = q.list(); StringBuffer sb = new StringBuffer(); for (TestRen a : (List<TestRen>) l) { sb.append(a.getOid() + " " + a.getName() + "\n"); } System.out.println(sb.toString()); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); HibernateUtil.release(); } } } 对象状态:暂态 持久态 游离态的转换图 hibernate 实现电子银行 package com.tarena.ebank.biz; public class Account { private Long oid; private String actNo; private double bal; public void deposite(double amount){ bal = bal + amount; } public void withdraw(double amount){ bal = bal - amount; } public Account(String actNo, double bal) { super(); this.actNo = actNo; this.bal = bal; } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public double getBal() { return bal; } public void setBal(double bal) { this.bal = bal; } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public Account() { super(); } } package com.tarena.ebank.biz; public interface IAccountService { /*增加账户,删除账户,查询余额,存款,取款,转帐*/ public void createAccount(Account act); public void removeAccount(String actNo); public double getBal(String actNo); public void deposite(String actNo,double amount); public void withdraw(String actNo,double amount); public void transfer(String from ,String to, double amount); } public class AccountServiceHbnImpl implements IAccountService { private IAccountDAO dao = AccountDAOFactory.getDAO("hbn"); @Override public void createAccount(Account act) { Session s = null; try { s = HbnUtil.getSessionFactory().openSession(); s.beginTransaction(); dao.insert(act, s); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); }finally{ s.close(); } } @Override public void deposite(String actNo, double amount) { Session s = null; try { s = HbnUtil.getSessionFactory().openSession(); s.beginTransaction(); Account a = dao.findAccountByActNo(actNo, s); a.deposite(amount); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); }finally{ s.close(); } } @Override public double getBal(String actNo) { Session s = null; double bal = 0.0; try { s = HbnUtil.getSessionFactory().openSession(); s.beginTransaction(); Account a = dao.findAccountByActNo(actNo, s); bal = a.getBal(); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); }finally{ s.close(); } return bal; } @Override public void removeAccount(String actNo) { Session s = null; try { s = HbnUtil.getSessionFactory().openSession(); s.beginTransaction(); dao.del(actNo, s); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); }finally{ s.close(); } } @Override public void transfer(String from, String to, double amount) { Session s = null; try { s = HbnUtil.getSessionFactory().openSession(); s.beginTransaction(); dao.findAccountByActNo(from, s) .withdraw(amount); dao.findAccountByActNo(to, s) .deposite(amount); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); }finally{ s.close(); } } @Override public void withdraw(String actNo, double amount) { Session s = null; try { s = HbnUtil.getSessionFactory().openSession(); s.beginTransaction(); Account a = dao.findAccountByActNo(actNo, s); a.withdraw(amount); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); }finally{ s.close(); } } } package com.tarena.ebank.biz; public class AccountServiceFactory { public static IAccountService getService(String type){ IAccountService s = null; if("hbn".equalsIgnoreCase(type)){ s = new AccountServiceHbnImpl(); }else if("jdbc".equalsIgnoreCase(type)){ s = new AccountServiceJdbcImpl(); } return s; } } <?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 package="com.tarena.ebank.biz"> <class name="Account" table="t_ebank_act"> <id name="oid" column="OID" > <generator class="seqhilo"></generator> </id> <property name="actNo" column="ACTNO" unique="true" not-null="true"></property> <property name="bal" column="BALANCE" not-null="true" /> </class> </hibernate-mapping> public interface IAccountDAO { public void insert(Account act,Session s) throws DataException; public void del(String actNo,Session s) throws DataException; public void update(Account act, Session s) throws DataException; public Account findAccountByActNo(String actNo,Session s) throws DataException; } public class AccountDAOHbnImpl implements IAccountDAO { @Override public void del(String actNo, Session s) throws DataException { try { s.delete(findAccountByActNo(actNo,s)); } catch (HibernateException e) { e.printStackTrace(); throw new DataException("del error"); } } @Override public Account findAccountByActNo(String actNo, Session s) throws DataException { Account a = null; try { String hql = "from Account a " + "where a.actNo=?"; a = (Account)s.createQuery(hql) .setString(0, actNo) .uniqueResult(); } catch (HibernateException e) { e.printStackTrace(); throw new DataException("find error"); } return a; } @Override public void insert(Account act, Session s) throws DataException { try { s.saveOrUpdate(act); } catch (HibernateException e) { e.printStackTrace(); throw new DataException("save or update error"); } } @Override public void update(Account act, Session s) throws DataException { insert(act,s); } } public class AccountDAOFactory { public static IAccountDAO getDAO(String type){ IAccountDAO dao = null; if("hbn".equalsIgnoreCase(type)){ dao = new AccountDAOHbnImpl(); }else if("jdbc".equalsIgnoreCase (type)){ dao = new AccountDAOJdbcImpl(); } return dao; } } import com.tarena.ebank.biz.Account; import com.tarena.ebank.biz.AccountServiceFactory; import com.tarena.ebank.biz.IAccountService; public class Test { public static void main(String[] args) { IAccountService s = AccountServiceFactory.getService("hbn"); s.createAccount(new Account("a-001",12000.0)); s.createAccount(new Account("a-002",7000.0)); StringBuffer sb = new StringBuffer(); sb.append("a-001="+s.getBal("a-001")+"\n"); sb.append("a-002="+s.getBal("a-002")+"\n"); sb.append("从a-001转756.0到a-002\n"); s.transfer("a-001", "a-002", 756.0); sb.append("a-001="+s.getBal("a-001")+"\n"); sb.append("a-002="+s.getBal("a-002")+"\n"); System.out.print(sb.toString()); } } **************************************************************** 第三天 关联映射-基数映射-1:1 非常重要 注意的设计(Account3 主键) 唯一外健映射: public class Account3 { private Long oid; private String actNo; private double bal; private User3 owner; public void deposite(double amount) { bal = bal + amount; } public void withdraw(double amount) { bal = bal - amount; } public Account3(String actNo, double bal) { this.actNo = actNo; this.bal = bal; } public Account3() { super(); } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public double getBal() { return bal; } public void setBal(double bal) { this.bal = bal; } public User3 getOwner() { return owner; } public void setOwner(User3 owner) { this.owner = owner; } } public class User3 { private Long oid; private String uid; private String name; private Account3 act; public User3() { } public User3(String uid, String name) { this.uid = uid; this.name = name; } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Account3 getAct() { return act; } public void setAct(Account3 act) { this.act = act; act.setOwner(this);////////////注意 为Account中添加USER3 } } <?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 package="hibernate"> <class name="Account3" table="t_act"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="actNo" column="ACTNO" not-null="true" unique="true"/> <property name="bal" column="BALANCE" not-null="true"/> <one-to-one name="owner" property-ref="act" cascade="save-update"></one-to-one>//主键 </class> </hibernate-mapping> <?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 package="hibernate"> <class name="User3" table="t_user"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="uid" column="USERID" not-null="true" unique="true"/> <property name="name" column="NAME" not-null="true"/> <many-to-one name="act" column="fid" unique="true" cascade="all"></many-to-one>//外健 </class> </hibernate-mapping> <?xml version="1.0"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@192.168.7.88:1521:tarena</property> <property name="connection.username">rensx</property> <property name="connection.password">rensx</property> <property name="connection.isolation">2</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="myeclipse.connection.profile">rensx_oracle</property> <property name="hbm2ddl.auto">create</property> <mapping resource="hibernate/Account3.hbm.xml" /> <mapping resource="hibernate/User3.hbm.xml" /> </session-factory> </hibernate-configuration> 测试类******************* public class TestAcconut3 { public static void main(String[] args) { User3 u = new User3("u-001", "ren"); Account3 a = new Account3("act-001", 200.0); Session s = null; s = HibernateUtil.getSessionFactory().openSession(); s.beginTransaction(); u.setAct(a); try { s.save(u); s.getTransaction().commit(); s.beginTransaction(); String hql = "from User3 a where a.uid = ?"; Query q = s.createQuery(hql); User3 user = (User3) q.setString(0, "u-001").uniqueResult(); StringBuffer sb = new StringBuffer(); sb.append(user.getName() + ">>>>" + user.getAct().getBal()); System.out.println(sb.toString()); s.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); HibernateUtil.release(); } } } 共享主键方案:常用 Account oid 主键 User oid 主键 又是外健 <?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 package="hibernate"> <class name="Account" table="t_act"> <id name="oid" column="OID"> <generator class="hilo"> <param name="table">t_hilo</param> <param name="column">HI</param> </generator> </id> <property name="actNo" column="ACTNO"/> <property name="bal" column="BALANCE"/>//去除property-ref="act" </class> </hibernate-mapping> <?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 package="hibernate"> <class name="User3" table="t_user"> <id name="oid" column="OID"> <generator class="foreign">//写法 <param name="property">act</param>//写法 </generator> </id> <property name="uid" column="USERID" not-null="true" unique="true"/> <property name="name" column="NAME" not-null="true"/> <one-to-one name="act" cascade="all" constrained="true"></one-to-one>//写法 </class> </hibernate-mapping> 别的都一样 ************************************************************* 第四天 ont-to-many关系: 注意每个类中新添加的东西 package hibernate; public class Account4 { private Long oid; private String actNo; private double bal; private User4 owner; public Account4(String actNo, double bal) { super(); this.actNo = actNo; this.bal = bal; } public void deposite(double amount){ bal = bal + amount; } public void withdraw(double amount){ bal = bal - amount; } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public double getBal() { return bal; } public void setBal(double bal) { this.bal = bal; } public User4 getOwner() { return owner; } public void setOwner(User4 owner) { this.owner = owner; } public Account4() { super(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((actNo == null) ? 0 : actNo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Account4 other = (Account4) obj; if (actNo == null) { if (other.actNo != null) return false; } else if (!actNo.equals(other.actNo)) return false; return true; } } package hibernate; import java.util.HashSet; import java.util.Set; public class User4 { private Long oid; private String uid; private String name; private Set<Account4> acts = new HashSet<Account4> (); public User4(String uid, String name) { super(); this.uid = uid; this.name = name; } public void addAct(Account4 act){ acts.add(act); act.setOwner(this); } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Account4> getActs() { return acts; } public void setActs(Set<Account4> acts) { this.acts = acts; } public User4() { super(); } } <?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 package="hibernate"> <class name="Account4" table="t_act"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="actNo" column="ACTNO" not-null="true" unique="true"/> <property name="bal" column="BALANCE" not-null="true"/> <many-to-one name="owner" column="fid" cascade="save-update"></many-to-one> </class> </hibernate-mapping> <?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 package="hibernate"> <class name="User4" table="t_user"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="uid" column="USERID" not-null="true" unique="true"/> <property name="name" column="NAME" not-null="true"/> <set name="acts" cascade="all" inverse="true">//inverse 默认为false true为交出维护权 <key column="fid"/>//指明外健 Account4的fid为外健 <one-to-many class="Account4"></one-to-many>//说明set里的类型 </set> </class> </hibernate-mapping> package hibernate; import org.hibernate.HibernateException; import org.hibernate.Session; public class TestAccount4 { public static void main(String[] args) { User4 u = new User4("u-001", "Mike"); Account4 a = new Account4("act-001", 3000.0); u.addAct(a); a = new Account4("act-002",8000.0); u.addAct(a); Session s = null; try { s = HibernateUtil.getSessionFactory().openSession(); s.beginTransaction(); s.save(a); s.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); HibernateUtil.release(); } } } package hibernate; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; public class TestAccount4_2 { public static void main(String[] args) { Session s=null; try { s = HibernateUtil.getSessionFactory().openSession(); s.beginTransaction(); User4 u=(User4)s.get(User4.class, 20L); if(u==null){System.out.println("null");} else {System.out.println(u.getName());} // String hql="delete User4 a where a.name=?"; // Query q=s.createQuery(hql).setString(0, u.getName()); // q.executeUpdate(); s.delete(u);//可以级联删除 在 XML文件中的配置的级联删除只使用session的delete 是先删子表 // HQL不可级联删除 因为XML文件中的配置的级联删除不影响数据库的表结构的级联删除 s.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); s.getTransaction().rollback(); } finally { s.close(); HibernateUtil.release(); } } } 下午 many-to-many****************************** package hibernate.course; import java.util.HashSet; import java.util.Set; public class Student { private Long oid; private String sid; private String name; private Set cours = new HashSet(); public Student(String sid, String name) { super(); this.sid = sid; this.name = name; } public Student() { super(); } public void registerCour(Course c){ cours.add(c); c.getStus().add(this); } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getCours() { return cours; } public void setCours(Set cours) { this.cours = cours; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((sid == null) ? 0 : sid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Student other = (Student) obj; if (sid == null) { if (other.sid != null) return false; } else if (!sid.equals(other.sid)) return false; return true; } } package hibernate.course; import java.util.HashSet; import java.util.Set; public class Course { private Long oid; private String cid; private String name; private Set Stus = new HashSet(); public Course(String cid, String name) { super(); this.cid = cid; this.name = name; } public Course() { super(); } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getCid() { return cid; } public void setCid(String cid) { this.cid = cid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getStus() { return Stus; } public void setStus(Set stus) { Stus = stus; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((cid == null) ? 0 : cid.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Course other = (Course) obj; if (cid == null) { if (other.cid != null) return false; } else if (!cid.equals(other.cid)) return false; return true; } } <?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 package="hibernate.course"> <class name="Course" table="t_cour"> <id name="oid" column="OID" > <generator class="seqhilo" /> </id> <property name="cid" column="CID" unique="true" not-null="true"></property> <property name="name" column="NAME" not-null="true"/> <set name="stus" table="t_enrollment" cascade="save-update"> <key column="cfid" />//做为t_enrollment的外健 指向Course的主键 <many-to-many class="Student" column="sfid"/> </set> </class> </hibernate-mapping> <?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 package="hibernate.course"> <class name="Student" table="t_stu"> <id name="oid" column="OID" > <generator class="seqhilo" /> </id> <property name="sid" column="SID" unique="true" not-null="true"></property> <property name="name" column="NAME" not-null="true"/> <set name="cours" table="t_enrollment" cascade="save-update" inverse="true"> <key column="sfid" /> <many-to-many class="Course" column="cfid"/> </set> </class> </hibernate-mapping> package hibernate.course; import java.util.Random; import org.hibernate.HibernateException; import org.hibernate.Session; public class TestManyToMany { public static void main(String[] args) { String[] sname = {"Mike","Tony","Jack","Rose","Black"}; String[] cname = {"Core Java","JDBC","Hibernate","Spring","JSP","EJB"}; Student[] s = new Student[sname.length]; Course[] c = new Course[cname.length]; for(int i=0;i<sname.length;i++){ s[i] = new Student("s-00"+i,sname[i]); } for(int i=0;i<cname.length;i++){ c[i] = new Course("c-00"+i,cname[i]); } for(int i=0;i<sname.length;i++){ for(int j=0;j<cname.length;j++){ if(new Random().nextInt()>0){ s[i].registerCour(c[j]); } } } Session ss = null; try { ss = HibernateUtil.getSessionFactory().openSession(); ss.beginTransaction(); ss.save(s[1]); ss.getTransaction().commit(); } catch (HibernateException e) { e.printStackTrace(); ss.getTransaction().rollback(); }finally{ ss.close(); HibernateUtil.release(); } } } 单一组件 双方为组合关系才可以使用单一组件 package hibernate.course; //组件单一 public class Account { private String actNo; private double bal;//属性的写法 无OID 实体类属性 public Account(String actNo, double bal) { this.actNo = actNo; this.bal = bal; } public Account() { super(); } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public double getBal() { return bal; } public void setBal(double bal) { this.bal = bal; } } package hibernate.course; public class User { private Long oid;// private String uid; private String name; private Account act;//含有一个实体类 public User() { } public User(String uid, String name) { this.uid = uid; this.name = name; } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Account getAct() { return act; } public void setAct(Account act) { this.act = act; } } <?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 package="hibernate.course"> <class name="User" table="t_user">//只写含有实体类属性的那个类的XML映射文件 <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="uid" column="USERID" not-null="true" unique="true"/> <property name="name" column="NAME" not-null="true"/> <component name="act">//与类的属性相对应 <property name="actNo" column="ACTNO" /> <property name="bal" column="BAL" /> </component> </class> </hibernate-mapping> 集合组件one-to-many package hibernate.test2; //集合组件 public class Account { private String actNo; private double bal; public Account(String actNo, double bal) { this.actNo = actNo; this.bal = bal; } public Account() { super(); } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public double getBal() { return bal; } public void setBal(double bal) { this.bal = bal; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((actNo == null) ? 0 : actNo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Account other = (Account) obj; if (actNo == null) { if (other.actNo != null) return false; } else if (!actNo.equals(other.actNo)) return false; return true; } } package hibernate.test2; import java.util.HashSet; import java.util.Set; //集合组件 public class User { private Long oid; private String uid; private String name; private Set acts = new HashSet(); public void addAccount(Account act){ acts.add(act); } public User() { } public User(String uid, String name) { this.uid = uid; this.name = name; } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getActs() { return acts; } public void setActs(Set acts) { this.acts = acts; } } <?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 package="hibernate.test2"> <class name="User" table="t_user2"> <id name="oid" column="OID" > <generator class="seqhilo"></generator> </id> <property name="uid" column="USERID" type="string" unique="true" not-null="true"></property> <property name="name" column="NAME" not-null="true" /> <set name="acts" table="t_acts2"> <key column="fid" /> <composite-element class="Account"> <property name="actNo" column="ACTNO" unique="true" not-null="true" /> <property name="bal" column="BAL" /> </composite-element> </set> </class> </hibernate-mapping> *************************************************************** 第五天 3种继承映射的实现 一个类一个表: package hibernate.test2; public abstract class Account { private Long oid; private String actNo; private double bal; private User owner; public Account(String actNo, double bal) { super(); this.actNo = actNo; this.bal = bal; } public void deposite(double amount){ bal = bal + amount; } public void withdraw(double amount){ bal = bal - amount; } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getActNo() { return actNo; } public void setActNo(String actNo) { this.actNo = actNo; } public double getBal() { return bal; } public void setBal(double bal) { this.bal = bal; } public User getOwner() { return owner; } public void setOwner(User owner) { this.owner = owner; } public Account() { super(); } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((actNo == null) ? 0 : actNo.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Account other = (Account) obj; if (actNo == null) { if (other.actNo != null) return false; } else if (!actNo.equals(other.actNo)) return false; return true; } //*************** public void getMon(double mon){ bal=bal-mon; } } package hibernate.test2; public class CheckAccount extends Account { private double check;// public CheckAccount(){} public CheckAccount(String actNo, double bal, double check) { super(actNo, bal); this.check = check; } public CheckAccount(String actNo, double bal) { super(actNo, bal); } public void getMon(double mon){ double bal=getBal(); if(bal+check>mon){ bal=bal-mon; setBal(bal); } } public double getCheck() { return check; } public void setCheck(double check) { this.check = check; } } package hibernate.test2; public class DebitAccount extends Account { private double debit; public DebitAccount(){} public DebitAccount(String actNo, double bal) { super(actNo, bal); } public DebitAccount(String actNo, double bal, double debit) { super(actNo, bal); this.debit = debit; } public void getMon(double mon){ double bal=getBal(); if(bal-debit>=mon){ bal=bal-mon; setBal(bal); } } public double getDebit() { return debit; } public void setDebit(double debit) { this.debit = debit; } } package hibernate.test2; import java.util.HashSet; import java.util.Set; public class User { private Long oid; private String uid; private String name; private Set<Account> acts = new HashSet<Account> (); public User(String uid, String name) { super(); this.uid = uid; this.name = name; } public void addAct(Account act){ acts.add(act); act.setOwner(this); } public Long getOid() { return oid; } public void setOid(Long oid) { this.oid = oid; } public String getUid() { return uid; } public void setUid(String uid) { this.uid = uid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Account> getActs() { return acts; } public void setActs(Set<Account> acts) { this.acts = acts; } public User() { super(); }} <?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 package="hibernate.test2"> <class name="Account" table="t_act"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="actNo" column="ACTNO" not-null="true" unique="true"/> <property name="bal" column="BALANCE" not-null="true"/> <many-to-one name="owner" column="fid" cascade="save-update"></many-to-one> <joined-subclass name="CheckAccount" table="t_checkAct"> <key column ="oid"/> <property name="check" column="ch" not-null="true"/> </joined-subclass> <joined-subclass name="DebitAccount" table="t_debitAct"> <key column ="oid"/> <property name="debit" column="DEBIT" not-null="true"/> </joined-subclass> </class> </hibernate-mapping> <?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 package="hibernate.test2"> <class name="User" table="t_user"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="uid" column="USERID" not-null="true" unique="true"/> <property name="name" column="NAME" not-null="true"/> <set name="acts" cascade="all" inverse="true"> <key column="fid"/> <one-to-many class="Account"></one-to-many> </set> </class> </hibernate-mapping> 一个类库一个表: 别的跟上面一样 <?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 package="hibernate.test2"> <class name="Account" table="t_act"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <discriminator column="TYPE"></discriminator>// <property name="actNo" column="ACTNO" not-null="true" unique="true"/> <property name="bal" column="BALANCE" not-null="true"/> <many-to-one name="owner" column="fid" cascade="save-update"></many-to-one> <subclass name="CheckAccount" discriminator-value="c"> <property name="check" column="ch"></property> </subclass> <subclass name="DebitAccount" discriminator-value="d"> <property name="debit" column="DEBIT"></property> </subclass> </class> </hibernate-mapping> 一个实体类一个表: 别的跟上面一样 <?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 package="hibernate.test2"> <class name="Account" table="t_act"> <id name="oid" column="OID"> <generator class="seqhilo"> </generator> </id> <property name="actNo" column="ACTNO" not-null="true" unique="true"/> <property name="bal" column="BALANCE" not-null="true"/> <many-to-one name="owner" column="fid" cascade="save-update"></many-to-one> <union-subclass name="CheckAccount" table="t_check"> <property name="check" column="ch" not-null="true"/> </union-subclass> <union-subclass name="DebitAccount" table="t_debit"> <property name="debit" column="DEBIT" not-null="true" /> </union-subclass> </class> </hibernate-mapping> ************************************************* public class HbnUtil { private static SessionFactory sf; private static ThreadLocal<Session> local = new ThreadLocal<Session>(); public static SessionFactory getSessionFactory() { if (sf == null) { try { sf = new Configuration().configure().buildSessionFactory(); } catch (HibernateException e) { e.printStackTrace(); } } return sf; } public static Session getSession() { Session s = (Session) local.get(); if (s == null) { s = getSessionFactory().openSession(); local.set(s); } return s; } public static void closeSession() { Session s = (Session) local.get(); if (s != null) { s.close(); local.set(null); } } public static void closeSessionFactory() { if (sf != null) { sf.close(); sf = null; } } } ThreadLocal解决借口污染的电子银行代码
发表评论
-
解决数据库乱码问题
2009-12-11 15:55 667MySql中的my.ini 文件中default-charac ... -
Hibernate_北京老师串讲内容
2009-10-07 20:48 836话题一:hibernate的基本使用 学习方向:重点理解hi ... -
Hibernate_整理资料
2009-08-09 19:44 1112http://hi.baidu.com/hking1987/b ... -
Hibernate_Session_delete方法
2009-08-02 22:16 894delete()方法用于从数据库中删除与Java对象对应的记录 ... -
Hibernate_缓存机制
2009-08-02 21:53 1401这是在一次事务提交时遇到的异常。 an assertion ... -
JDBC_ResultSet对象介绍
2009-08-02 14:12 1749http://hi.baidu.com/ichooseilik ... -
JDBC_常用类和方法
2009-08-02 13:57 1714JDBC常用类和方法 一、 ... -
JDBC_上课自己的代码
2009-08-02 13:54 1117第一天 连接Oracle package tarena; ... -
JDBC_银行代码
2009-07-27 21:24 1349//1 public interface IAccountS ...
相关推荐
Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,它将POJO与数据库表建立映射关系,是一个全自动的orm框架,hibernate可以自动生成SQL语句,自动执行,使得Java程序员可以...
熟练运用Hibernate进行增删改查,很快掌握Hibernate
给学生上课时自己用存java代码实现的仿照hibernate框架的代码,适合初学者。
达内笔记 内有java ajax jsp hibernate struts servlet html jdbc oracle xml Javascript 等上课的笔记 感想,上课小结截图 以及上课的代码。
与本课件相关的源代码程序为Struts2Demo、Spring2Demo、Hibernate3Demo、Struts2Spring2Demo、Struts2Spring2Hibernate3Demo、SSHDemo项目。 本课件可以任意传播,但是不允许修改其内容;同时引用该课件的内容,请...
代码+数据库,struts2.3,hibernate4.3,mysql-connector-java-5.1.39-bin 代码实现功能包括: 教师和学生的注册及登录功能 系统使用人员的权限: 教师可以修改选课人数、课程名称、上课时间、上课地点、录入成绩、...
【服务器端采用SSH框架,请自己启动tomcat服务器,hibernate会自动生成数据库表的哈!】 hibernate生成数据库表后,只需要在admin管理员表中加个测试账号密码就可以登录后台了哈! 下面是数据库的字段说明: 班级...
这些是该人的上课笔记可以大家参考下 用于实现在程序中处理java代码与数据库之间的数据操作的一种工具类,那么这个类中会提供4种通用类型的方法,包括:增,删,改,查
【服务器端采用SSH框架,请自己启动tomcat服务器,hibernate会自动生成数据库表的哈!】 hibernate生成数据库表后,只需要在admin管理员表中加个测试账号密码就可以登录后台了哈! 下面是数据库的字段说明: 学生...
【服务器端采用SSH框架,请自己启动tomcat服务器,hibernate会自动生成数据库表的哈!】 hibernate生成数据库表后,只需要在admin管理员表中加个测试账号密码就可以登录后台了哈! 下面是数据库的字段说明: 学院...
(1)aop:面向切面编程,扩展功能不是修改源代码实现 (2)ioc:控制反转, - 比如有一个类,在类里面有方法(不是静态的方法),调用类里面的方法,创建类的对象,使用对象调用方法,创建类对象的过程,需要new...
课程信息管理:管理员可以轻松添加、编辑和删除课程信息,包括课程名称、授课教师、上课时间、上课地点等。 学生选课功能:学生可以根据自己的兴趣和课程安排,在线浏览并选择课程。系统能够实时显示课程的剩余名额...
本课程共15讲,课程课件,上课源代码等资料购买后可以直接下载,该课程是在学习完JSP,Struts2,Hibernate课程后,SSH框架的第三部分,后续还有Spring MVC课程。本次课程的主要内容:掌握系统框架设计原理,工厂模式,...
Spring Boot课程存储库。 所有代码均在上课期间产生。 老师:Nelio Alves。
本课程共21讲,课程课件,上课源代码等资料购买后可以直接下载,该课程是在学习完JSP课程后,SSH框架的第一部分,后续还有Hibernate,Spring课程及3大框架的整合
掌握必要的细节,这个不含糊,至于其他,或者通过视频给出(这样可以给出更多的细节,但是不占上课时间,课上只讲重要的、必要的细节知识),或者在掌握了自学的能力后自己能够查出,这才是正途。 当你看书到某个...