BrijBhasin
asked on
Hibernate XMl persistence
Hi,
I found this article that talks about XML persistence with Java.
http://www.devx.com/Java/Article/27896/0/page/2
how would I change this code to read an xml file that resides in the same directory as my java file.
document = saxReader.read(inputXML);
I found this article that talks about XML persistence with Java.
http://www.devx.com/Java/Article/27896/0/page/2
how would I change this code to read an xml file that resides in the same directory as my java file.
document = saxReader.read(inputXML);
ASKER
it says saxreader can't be resolved and Cannot make a static reference to the non-static method getClass() from the type Object
whats the rest of your code look like, thats only a snippet in the tutorial
ASKER
sorry about the sax error .. but i'm still getting the error in my class
SAXReader reader = new SAXReader();
//Document document = SAXReader.read(Contract.xm l);
Document document = reader.read(getClass().get ResourceAs Stream("ab c.xml"));
List clients = document.selectNodes("//Cl ient");
List contracts = document.selectNodes("//Co ntract");
try {
Session session = InitSessionFactory.getInst ance().get CurrentSes sion();
Session dom4jSession = session.getSession(EntityM ode.DOM4J) ;
Transaction transaction = session.beginTransaction() ;
Iterator iter = clients.iterator();
while (iter.hasNext()) {
Object next = iter.next();
dom4jSession.saveOrUpdate( "general.C lient", next );
}// end while
Iterator iter2 = contracts.iterator();
while (iter.hasNext()) {
Object next = iter2.next();
dom4jSession.saveOrUpdate( "general.C ontract", next );
}// end while
transaction.commit();
session.close();}
finally{
}
SAXReader reader = new SAXReader();
//Document document = SAXReader.read(Contract.xm
Document document = reader.read(getClass().get
List clients = document.selectNodes("//Cl
List contracts = document.selectNodes("//Co
try {
Session session = InitSessionFactory.getInst
Session dom4jSession = session.getSession(EntityM
Transaction transaction = session.beginTransaction()
Iterator iter = clients.iterator();
while (iter.hasNext()) {
Object next = iter.next();
dom4jSession.saveOrUpdate(
}// end while
Iterator iter2 = contracts.iterator();
while (iter.hasNext()) {
Object next = iter2.next();
dom4jSession.saveOrUpdate(
}// end while
transaction.commit();
session.close();}
finally{
}
replace getClass() with CLASSNAME.class
where CLASSNAME is the name of the class making the call
where CLASSNAME is the name of the class making the call
ASKER
I'm getting this error for this line.
Document document = reader.read(RunHibernate.c lass.getRe sourceAsSt ream("Cont ract.xml") );
Unhandled exception type DocumentException
Document document = reader.read(RunHibernate.c
Unhandled exception type DocumentException
ASKER
I unable to find an answer to this error online: Unhandled exception type DocumentException
Increasing points .. please help
Increasing points .. please help
> RunHibernate.class.getReso urceAsStre am("Contra ct.xml")
can you check what value this returns
can you check what value this returns
ASKER
tried this
public Document parseUsingSAX() throws DocumentException, Exception
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("Contract.xml"));
return document;
}
RunHibernate R = new RunHibernate();
Document document = R.parseUsingSAX(); // getting Unhandled exception type Exception
public Document parseUsingSAX() throws DocumentException, Exception
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("Contract.xml"));
return document;
}
RunHibernate R = new RunHibernate();
Document document = R.parseUsingSAX(); // getting Unhandled exception type Exception
where exactly is the file?
ASKER
The Contract.xml file is the same directory as the RunHibernate.java file
Are you running the application from that directory?
How are you running it?
How are you running it?
ASKER
First I'm trying to build it and Eclipse won't let me do that because of the above error.
try
{
Document document = R.parseUsingSAX(); // getting Unhandled exception type Exception
}
catch (Exception ex)
{
ex.printStackTrace();
}
{
Document document = R.parseUsingSAX(); // getting Unhandled exception type Exception
}
catch (Exception ex)
{
ex.printStackTrace();
}
ASKER
Replaced with above code.
Now I get error on this line
List clients = document.selectNodes("//Cl ient"); // document cannot be resolved
Now I get error on this line
List clients = document.selectNodes("//Cl
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok .. I did that and worked through some of the errors in my java code and my hibernate mappin files and now I'm stuck at this error. below the error is my code and the xml file that I'm trying to persist.
----
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environ ment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(CompanyID) from clients
Hibernate: insert into clients (CompanyName, Address1, City, State, Zip, CompanyID) values (?, ?, ?, ?, ?, ?)
org.hibernate.SessionExcep tion: Session was already closed
at org.hibernate.impl.Session Impl.close (SessionIm pl.java:27 0)
at sun.reflect.NativeMethodAc cessorImpl .invoke0(N ative Method)
at sun.reflect.NativeMethodAc cessorImpl .invoke(Un known Source)
at sun.reflect.DelegatingMeth odAccessor Impl.invok e(Unknown Source)
at java.lang.reflect.Method.i nvoke(Unkn own Source)
at org.hibernate.context.Thre adLocalSes sionContex t$Transact ionProtect ionWrapper .invoke(Th readLocalS essionCont ext.java:3 01)
at $Proxy0.close(Unknown Source)
at RunHibernate.createXMLPers ist(RunHib ernate.jav a:134)
at RunHibernate.main(RunHiber nate.java: 53)
RunHibernate R = new RunHibernate();
try
{
Document document = R.parseUsingSAX(); // getting Unhandled exception type Exception
List clients = document.selectNodes("//Cl ient");
List contracts = document.selectNodes("//Co ntract");
Session session = InitSessionFactory.getInst ance().get CurrentSes sion();
Transaction transaction = session.beginTransaction() ;
Session dom4jSession = session.getSession(EntityM ode.DOM4J) ;
Iterator iter = clients.iterator();
while (iter.hasNext()) {
Object next = iter.next();
dom4jSession.saveOrUpdate( "general.c lients.Cli ent", next );
}// end while
Iterator iter2 = contracts.iterator();
while (iter.hasNext()) {
Object next = iter2.next();
dom4jSession.saveOrUpdate( "general.c ontracts.C ontract", next );
}// end while
transaction.commit();
session.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
-------------------------- --------
<?xml version="1.0"?>
<PontusContract>
<Client>
<CompanyName>FICO</Company Name>
<Address1 Type="Main">500 N rd St.</Address1>
<City>Minneapolis</City>
<State>MN</State>
<Zip>55401</Zip>
</Client>
<Contract ID="1" ContractType="NDA" CompanyID="2">
<ContractTitle>XYZ NDA</ContractTitle>
<ContractDates>
<Date Type="ContractSign">1/1/06 </Date>
<Date Type="ContractExpiry">12/1 /06</Date>
</ContractDates>
<ClientRefNum>123</ClientR efNum>
<ContractCustomer>XYZ</Con tractCusto mer>
</Contract>
</PontusContract>
----
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environ
log4j:WARN Please initialize the log4j system properly.
Hibernate: select max(CompanyID) from clients
Hibernate: insert into clients (CompanyName, Address1, City, State, Zip, CompanyID) values (?, ?, ?, ?, ?, ?)
org.hibernate.SessionExcep
at org.hibernate.impl.Session
at sun.reflect.NativeMethodAc
at sun.reflect.NativeMethodAc
at sun.reflect.DelegatingMeth
at java.lang.reflect.Method.i
at org.hibernate.context.Thre
at $Proxy0.close(Unknown Source)
at RunHibernate.createXMLPers
at RunHibernate.main(RunHiber
RunHibernate R = new RunHibernate();
try
{
Document document = R.parseUsingSAX(); // getting Unhandled exception type Exception
List clients = document.selectNodes("//Cl
List contracts = document.selectNodes("//Co
Session session = InitSessionFactory.getInst
Transaction transaction = session.beginTransaction()
Session dom4jSession = session.getSession(EntityM
Iterator iter = clients.iterator();
while (iter.hasNext()) {
Object next = iter.next();
dom4jSession.saveOrUpdate(
}// end while
Iterator iter2 = contracts.iterator();
while (iter.hasNext()) {
Object next = iter2.next();
dom4jSession.saveOrUpdate(
}// end while
transaction.commit();
session.close();
}
catch (Exception ex)
{
ex.printStackTrace();
}
--------------------------
<?xml version="1.0"?>
<PontusContract>
<Client>
<CompanyName>FICO</Company
<Address1 Type="Main">500 N rd St.</Address1>
<City>Minneapolis</City>
<State>MN</State>
<Zip>55401</Zip>
</Client>
<Contract ID="1" ContractType="NDA" CompanyID="2">
<ContractTitle>XYZ NDA</ContractTitle>
<ContractDates>
<Date Type="ContractSign">1/1/06
<Date Type="ContractExpiry">12/1
</ContractDates>
<ClientRefNum>123</ClientR
<ContractCustomer>XYZ</Con
</Contract>
</PontusContract>
ASKER
so it inserts a client partly but not the contract
> while (iter.hasNext()) {
In second loop, that should be iter2
In second loop, that should be iter2
ASKER
ok that fixed the contract not being entered but the "Session was already closed" error remains .. also, these fields are not being populated in the database for some reason.
Address1
AddressType
ContractTitle
Address1
AddressType
ContractTitle
> session.close();
try removing that line
try removing that line
ASKER
also, I'm trying to enter this as an xml fragment into a MySQL field of type text, but when I open editor to see the data it shows up as a bunch of boxes .. so does this mean I can't enter XML in MySQL??
<Date Type="ContractSign">1/1/06 </Date>
<Date Type="ContractExpiry">12/1 /06</Date>
<Date Type="ContractSign">1/1/06
<Date Type="ContractExpiry">12/1
ASKER
ok the session error went away but some fields still are not being populated in the database.
check your hibernate mappings are ampping all required fields
ASKER
I have rechecked my mappings and they alll map correctly but for some reason it is still not entering these fields. Address1
AddressType
ContractTitle
I can't isolate why it would enter the others and not these even though all the field are setup in the java file and hibernate mappings similarly.
AddressType
ContractTitle
I can't isolate why it would enter the others and not these even though all the field are setup in the java file and hibernate mappings similarly.
If the data is in the object being save and it is mapped correctly then it should be being wrtten to the database.
ASKER
Thanks for all your help objects in gettting me close to the solution.. still haven't been able to figure the exact problem though.
document = saxReader.read(getClass().
if the xml is in same directory as it application is being run from then try
document = saxReader.read(new FileInputStream("abc.xml")