我用的是Oracle:两个表
-- 创建用户表
CREATE TABLE SCOTT.bloguser
(
id NUMBER NOT NULL,
username VARCHAR2(50) UNIQUE NOT NULL,
password VARCHAR2(50) NOT NULL,
CONSTRAINT PK_bloguser PRIMARY KEY (id )
);
-- 创建用户表主键序列
CREATE SEQUENCE SEQ_BLOGUSERID START WITH 1 INCREMENT BY 1 MINVALUE 1 NOCACHE NOCYCLE NOORDER ;
-- 创建bolg表
CREATE TABLE SCOTT.bolginfo
(
id NUMBER,
userId NUMBER NOT NULL,
content VARCHAR2(1000),
CONSTRAINT PK_bolginfo PRIMARY KEY (id )
);
-- 创建bolg表主键序列
CREATE SEQUENCE SEQ_BLOGID START WITH 1 INCREMENT BY 1 MINVALUE 1 NOCACHE NOCYCLE NOORDER ;
--添加外键约束
ALTER TABLE SCOTT.bolginfo
ADD CONSTRAINT FK_userId_blogId FOREIGN KEY (userId) REFERENCES SCOTT.bloguser(id);
Bloguser.java
package com.jam.hibeinate.po;
import java.util.Set;
/**
* Bloguser entity.
*
* @author Jam Chan
*/
public class Bloguser implements java.io.Serializable {
private Long id;
private String username;
private String password;
private Set bolginfos = new HashSet(0);
}
Bolginfo.java
package com.jam.hibeinate.po;
/**
* Bolginfo entity.
*
* @author Jam Chan
*/
public class Bolginfo implements java.io.Serializable {
private Long id;
private Bloguser bloguser;
private String content;
}
Bloguser.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.jam.hibeinate.po.Bloguser" table="BLOGUSER" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">
SEQ_BLOGUSERID
</param>
</generator>
</id>
<property name="username" type="java.lang.String">
<column name="USERNAME" length="50" not-null="true" unique="true" />
</property>
<property name="password" type="java.lang.String">
<column name="PASSWORD" length="50" not-null="true" />
</property>
<set name="bolginfos" inverse="true" cascade="all" lazy="false" outer-join="auto">
<key>
<column name="USERID" precision="22" scale="0" not-null="true" />
</key>
<one-to-many class="com.jam.hibeinate.po.Bolginfo" />
</set>
</class>
</hibernate-mapping>
Bolginfo.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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.jam.hibeinate.po.Bolginfo" table="BOLGINFO" schema="SCOTT">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">SEQ_BLOGID</param>
</generator>
</id>
<many-to-one name="bloguser" class="com.jam.hibeinate.po.Bloguser" fetch="select" insert="true" update="true" outer-join="true">
<column name="USERID" precision="22" scale="0" not-null="true" />
</many-to-one>
<property name="content" type="java.lang.String">
<column name="CONTENT" length="1000" />
</property>
</class>
</hibernate-mapping>
注:Spring的applicationContext.xml事务代理管理的配置省略……
之前我实现多表查询的时候抛出
could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
……………………
的异常,如果想使Session is opened需要在
web.xml添加如下配置:
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!-- 选择需要过滤的页面 -->
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.faces</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
之后就OK啦~!!哈哈
posted on 2008-03-23 10:06
Jam Chan 阅读(957)
评论(0) 编辑 收藏 所属分类:
SSH整合