锘??xml version="1.0" encoding="utf-8" standalone="yes"?> 璀﹀憡: afterTransactionCompletion() was never called 鎵ц浜唗x = session.beginTransaction(); 浣嗘槸鍚庢潵娌″鐞唗x 搴旇鐢╰x.commit() or tx.rollback() 涓鑸煡璇㈡椂瀹規槗鍑虹幇姝ら棶棰?/P>
鍘熷洜鏄?BR>java.sql.Blob涓嶈兘寮哄埗浼犲敜鎴恛racle.sql.BLOB
瑙e喅鏂規硶濡備笅錛?BR>
SerializableClob lob=(SerializableClob)us.getStatisticsInfo();
CLOB lob2 = (CLOB)lob.getWrappedClob();
Writer out = lob2.getCharacterOutputStream();
public void testAdd() throws Exception
{
byte[] buffer = new byte[1];
buffer[0] = 1;
Session s = null;
try
{
s = sf.openSession();
Transaction tx = s.beginTransaction();
BizUserStatistics us= new BizUserStatistics();
us.setId(new Long(100));
us.setStatisticsInfo(Hibernate.createClob(" "));
s.save(us);
s.flush();
s.refresh(us, LockMode.UPGRADE);
// CLOB clob = (CLOB)us.getStatisticsInfo();
SerializableClob lob=(SerializableClob)us.getStatisticsInfo();
CLOB lob2 = (CLOB)lob.getWrappedClob();
Writer out = lob2.getCharacterOutputStream();
String fileName = "d:/process_log.sql";
File f = new File(fileName);
FileInputStream fin = new FileInputStream(f);
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 1000; i++)
{
sb.append("<data>" + i + "</data>");
}
out.write(sb.toString());
out.close();
s.flush();
tx.commit();
} catch (Exception e)
{
e.printStackTrace();
} finally
{
if (s != null)
try
{
s.close();
} catch (Exception e)
{}
}
}
]]>
unclosed connection, forgot to call close() on your session?
length="10" force="false" insert="true"/>
Region.hbm.xml(15)
org.xml.sax.SAXParseException: Attribute "insert" must be declared for element type "discriminator".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.addDTDDefaultAttrsAndValidate(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.handleStartElement(Unknown Source)
at org.apache.xerces.impl.dtd.XMLDTDValidator.emptyElement(Unknown Source)
at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(
Unknown Source)
at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.DTDConfiguration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
at org.jdom.input.SAXBuilder.build(Unknown Source)
at org.jdom.input.SAXBuilder.build(Unknown Source)
at org.jdom.input.SAXBuilder.build(Unknown Source)
at net.sf.hibernate.tool.hbm2java.CodeGenerator.main(CodeGenerator.java:100)
at net.sf.hibernate.tool.hbm2java.Hbm2JavaTask.processFile(Hbm2JavaTask.java:149)
at net.sf.hibernate.tool.hbm2java.Hbm2JavaTask.execute(Hbm2JavaTask.java:97)
at org.apache.tools.ant.Task.perform(Task.java:341)
at org.apache.commons.jelly.tags.ant.AntTag.doTag(AntTag.java:185)
at org.apache.commons.jelly.impl.TagScript.run(TagScript.java:233)
at org.apache.commons.jelly.impl.ScriptBlock.run(ScriptBlock.java:89)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag.runBodyTag(MavenGoalTag.java:79)
at org.apache.maven.jelly.tags.werkz.MavenGoalTag$MavenGoalAction.performAction(MavenGoalTag
.java:110)
at com.werken.werkz.Goal.fire(Goal.java:639)
at com.werken.werkz.Goal.attain(Goal.java:575)
at com.werken.werkz.Goal.attainPrecursors(Goal.java:488)
at com.werken.werkz.Goal.attain(Goal.java:573)
at org.apache.maven.plugin.PluginManager.attainGoals(PluginManager.java:671)
at org.apache.maven.MavenSession.attainGoals(MavenSession.java:263)
at org.apache.maven.cli.App.doMain(App.java:488)
at org.apache.maven.cli.App.main(App.java:1239)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.werken.forehead.Forehead.run(Forehead.java:551)
at com.werken.forehead.Forehead.main(Forehead.java:581)
build:
----------------------------------
----------------------------------
solution : 鍗囩駭hiberate verion 2.1.8
----------------------------------
----------------------------------
鏀筸aven 錛歱roject.xml
<dependency>
<groupId>hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>2.1.8</version>
<properties>
<ejb.manifest.classpath>true</ejb.manifest.classpath>
</properties>
</dependency>
榪欐牱鍐欙紝涔熸槸娌℃湁闂鐨?BR><discriminator type="java.lang.String" column="REGION_TYPE" length="10"/>
the Miner class we looked at was single table oriented, mapping to a single miners table. ORM solutions support ways to map associated tables to in memory objects ,
As a comparative example, lets look at the many to one relationship. We are going to expand our Deadwood example from part I. We add to the Miner a many to one association with a GoldClaim object. This means there is a foreign key, gold_claim_id in the miners table, which links it to a row in the gold_claims table.
Transitive Persistence
Its important for an ORM solution to provide a way to detect and cascade changes from in memory objects to the database, without the need to manually save() each one. Hibernate features a flexible and powerful version of this via declarative cascading persistence.
Deleting Hibernate offers a number of different cascading behaviors for all associations types, giving it a high degree of flexibility. For example, setting cascade="all" will make GoldClaim save, update and delete along with its parent Miner, like so...
Hibernate has its own object oriented query language (Hibernate Query Language - HQL), which is deliberately very similar to SQL. How it differs is that it lets developers express their queries in terms of objects and fields instead of tables and columns. Hibernate translates the query into SQL optimized for your particular database. Obviously, inventing a new query language is very substantial task, but the expressiveness and power of it is one of Hibernate's selling points.
Querying for Objects with HQL
when you have to start navigating across objects with SQL, HQL can be very convenient alternative. Let's take a look at our sample queries for HQL.
Having covered some of the basics of fetching objects, let's turn your attention to how we can make fetching objects fast. The next section covers the means by which we can tune the performance.
Beyond just mapping objects to tables, robust ORM solutions need to provide ways to tune the performance of the queries. One of the risks of working with ORM's is that you often pull back too much data from the database. This tends to happen because it its very easy to pull back several thousand rows, with multiple SQL queries, with a simple statement like "from Miner". Common ORM strategies for dealing with this include Lazy fetching, outer join fetching and caching.
What I mean by lazy is that when you fetch an object, the ORM tool doesn't fetch data from other tables, until you request the association. This prevents loading to much unneeded data. Hibernate allows you to choose which associations are lazy. This leads us to one of the great fallacies of ORM, that Lazy loading is always good. In reality, lazy loading is only good if you didn't need the data. Otherwise, you are doing with 2-1000+ queries what you could have done with one. This is dreaded N+1 select problem, where to get all the objects require N selects + 1 original selects. This problem gets much worse when you deal with collections..
Outer Joins and Explicit Fetching
Generally, one of the best way to improve performance is to limit the number of trips to the database. Better 1 big query than a few small ones. Hibernate has a number ways its handles the N+1 issue. Associations can be explicitly flagged for outer join fetching (via outer-join="true"), and you can add outer join fetching to HQL statements. For example...
While object caching isn't always going to be helpful or a performance silver bullet, Hibernate has a huge potential advantage here. It provides several levels of caching, including a session (UnitOfWork) level as well as an optional second level cache. You always use the '1st level' cache, as it prevents circular references and multiple trips to the database for the same object. Using a second level cache can allow much of the database state to stay resident in memory. This is especially useful for frequently read and reference data.