Blog信息 |
blog名称: 日志总数:1304 评论数量:2242 留言数量:5 访问次数:7599545 建立时间:2006年5月29日 |

| |
[Hibernate]hibernate对oracle的clob操作 软件技术, 电脑与网络
lhwork 发表于 2006/6/12 15:04:13 |
content.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"> <hibernate-mapping> <class name="oracle.Content" table="Content"> <id name="code" column="code" type="string" length="20"> <generator class="assigned"/> </id> <property name="title" column="title" type="string" length="100"/> <property name="annextitle" column="annextitle" type="string" length="100"/> <property name="comefrom" column="comefrom" type="string" length="100"/> <property name="author" column="author" type="string" length="100"/> <property name="content" column="content" type="clob" update="true" insert="true" /> </class> </hibernate-mapping>
content.java
package oracle;
import java.io.Serializable; import oracle.sql.*;import java.sql.Clob;
public class Content implements Serializable {
/** identifier field */ private String code;
/** nullable persistent field */ private String title;
/** nullable persistent field */ private String annextitle;
/** nullable persistent field */ private String comefrom;
/** nullable persistent field */ private String author;
private Clob content;
/** default constructor */ public Content() { } public Clob getContent() { return this.content; }
public void setContent( Clob content) { this.content = content; }
public String getCode() { return this.code; }
public void setCode( String code) { this.code = code; }
public String getTitle() { return this.title; }
public void setTitle( String title) { this.title = title; }
public String getAnnextitle() { return this.annextitle; }
public void setAnnextitle( String annextitle) { this.annextitle = annextitle; }
public String getComefrom() { return this.comefrom; }
public void setComefrom( String comefrom) { this.comefrom = comefrom; }
public String getAuthor() { return this.author; }
public void setAuthor( String author) { this.author = author; }
}测试代码
test.java
package oracle;
import junit.framework.TestCase;import net.sf.hibernate.*;import net.sf.hibernate.cfg.Configuration;import net.sf.hibernate.tool.hbm2ddl.SchemaExport;import oracle.sql.CLOB;
import java.io.Writer;
public class test extends TestCase{ private static SessionFactory _sessions = null;
public void testCreate() { try { Configuration cfg = new Configuration().configure("/xhr.hbm.xml"); SchemaExport dbExport = new SchemaExport(cfg); dbExport.create(true, true);
_sessions = cfg.buildSessionFactory(); } catch (MappingException e) { e.printStackTrace(); } catch (HibernateException e) { e.printStackTrace(); } }
public void testAdd() { try { Configuration conf = new Configuration().configure("/xhr.hbm.xml"); _sessions = conf.buildSessionFactory(); Session hSessions = _sessions.openSession(); Transaction tx = hSessions.beginTransaction(); Content con = new Content(); con.setCode("2004-12-20"); con.setTitle("IBM.WEBSPHERE.APPLICATION.SERVER.V6.0"); con.setAnnextitle("IBM.WEBSPHERE.APPLICATION.SERVER.V6.0"); con.setAuthor("IBM"); con.setComefrom("IBM"); con.setContent(Hibernate.createClob(" ")); hSessions.save(con); hSessions.flush();
hSessions.refresh(con, LockMode.UPGRADE); CLOB clob = (CLOB) con.getContent(); Writer out = clob.getCharacterOutputStream(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < 1000; i++) { sb.append("那里有下载"); } out.write(sb.toString()); out.close();
tx.commit(); hSessions.close();
} catch (Exception e) { e.printStackTrace(); }
}
}
xhr.hbm.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
<hibernate-configuration> <session-factory> <property name="dialect">net.sf.hibernate.dialect.OracleDialect</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@192.168.13.4:1521:hong</property> <property name="connection.username">system</property> <property name="connection.password">manager</property> <mapping resource="oracle/Content.xml"/> </session-factory>
</hibernate-configuration> |
|
|