File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Mapping Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Mapping Exception" Watch "Mapping Exception" New topic
Author

Mapping Exception

madhur taneja
Greenhorn

Joined: Jul 11, 2006
Posts: 4
I have created two java files - Team.java & Player.java with their respective tables - team , player .I have created the mapping files too and made the entry in configuration file too.The mapping files are:
Team.hbm.xml:-
<?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>
<class name="abc.hiber.Team" table="teams">

<id name="id" column="team_id" type="long" unsaved-value="null">
<generator class="assigned"/>
</id>

<property name="name" column="team_name" type="string" length="15" not-null="true"/>
<property name="city" column="city" type="string" length="15" not-null="true"/>

<set name="players" cascade="all" inverse="true" lazy="true">
<key column="team_id"/>
<one-to-many class="abc.hiber.Player"/>
</set>

</class>
</hibernate-mapping>

& Player.hbm.xml:-

<?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>

<class name="abc.hiber.Player" table="players">

<id name="id" column="player_id" type="long" unsaved-value="null">
<generator class="assigned"/>
</id>

<property name="firstName" column="first_name" type="string" length="12" not-null="true"/>
<property name="lastName" column="last_name" type="string" length="15" not-null="true"/>

<many-to-one name="team" class="abc.hiber.Team" column="team_id"/>
</class>
</hibernate-mapping>


The hibernate code is :


package abc.hiber;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import java.util.*;

public class teamexample

{
public static void main(String[] args)
{
Team tt = new Team();
tt.setId(1);
tt.setCity("london");
tt.setName("england");

Player pp = new Player();
pp.setId(1);
pp.setFirstName("Andrew");
pp.setLastName("flintof");

Set players = new HashSet();
players.add(pp);
tt.setPlayers(players);

Configuration cfg = new Configuration()
.addClass(Team.class)
.addClass(Player.class);
SessionFactory factory = cfg.configure("hibernate.cfg.xml").buildSessionFactory();
//SessionFactory factory = cfg.buildSessionFactory();
Session session =factory.openSession();
Transaction tx = null;

try
{
tx = session.beginTransaction();
System.out.println("Inserting Records ");
session.save(tt);
tx.commit();
session.close();
}

catch (HibernateException e)
{
e.printStackTrace();
if (tx != null && tx.isActive())
tx.rollback();
}

}
}
but iam getting the following error:-


org.hibernate.MappingException: Could not read mappings from resource: abc/hiber/Team.hbm.xml
at org.hibernate.cfg.Configuration.addClass(Configuration.java:506)
at abc.hiber.teamexample.main(teamexample.java:31)
Caused by: org.hibernate.MappingException: Could not parse mapping document in input stream
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:431)
at org.hibernate.cfg.Configuration.addClass(Configuration.java:503)
... 1 more
Caused by: org.dom4j.DocumentException: Error on line 2 of document : XML declaration may only begin entities. Nested exception: XML declaration may only begin entities.
at org.dom4j.io.SAXReader.read(SAXReader.java:482)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:422)
... 2 more
Exception in thread "main"
Can u help me .Any help would be appreciated
Thanks in Advance
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


XML declaration may only begin entities

Looks like you Team.hbm.xml is not valid XML. In particular you may have something in that file before the "<?xml version="1.0"?>" text.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
madhur taneja
Greenhorn

Joined: Jul 11, 2006
Posts: 4
thanks paul, but removing this line from .hbm.xml files did nt help .

well my package name is : abc.hiber , so i have used abc.hiber.Team.hbm.xml in the mapping document and in .cfg.xml file , is that correct or should i use Team.hbm.xml.

Also i have saved both .cfg.xml file &.hbm.xml in same folder , the error what came was Duplicate Mapping Exception , where should i place these files - in same folder that in hiber folder or one of these should be kept somewhere else.

thanks .
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Don't remove that line. You can only have one <?xml version="1.0"?> in a file, and you can't have anything in the file before <?xml version="1.0"?>. This is what your parsing exception is telling you. Your configuration must be OK, since Hibernate is finding Team.hbm.xml, but Team.hbm.xml is not valid xml.
madhur taneja
Greenhorn

Joined: Jul 11, 2006
Posts: 4
Thanks Paul , my problem has been figured .
The insert into Team is working , but entry in players table is not coming .
when i run the hibernate code , the following thing comes on console :



Inserting Records
Hibernate:
insert
into
teams
(team_name, city, team_id)
values
(?, ?, ?)
Hibernate:
update
players
set
first_name=?,
last_name=?,
team_id=?
where
player_id=?
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at abc.hiber.teamexample.main(teamexample.java:43)


insert into teams is fine , but why update in players and not insert , does it has to take something from - inverse="true" that i used in team.hbm.xml.
Also , why this stale state exception is coming ?

Thanks.
sankey jain
Ranch Hand

Joined: Apr 29, 2010
Posts: 58
madhur taneja wrote:Thanks Paul , my problem has been figured .
The insert into Team is working , but entry in players table is not coming .
when i run the hibernate code , the following thing comes on console :



Inserting Records
Hibernate:
insert
into
teams
(team_name, city, team_id)
values
(?, ?, ?)
Hibernate:
update
players
set
first_name=?,
last_name=?,
team_id=?
where
player_id=?
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at abc.hiber.teamexample.main(teamexample.java:43)


insert into teams is fine , but why update in players and not insert , does it has to take something from - inverse="true" that i used in team.hbm.xml.
Also , why this stale state exception is coming ?

Thanks.


Hello Madhur,

I am facing a similar issue, how did you solve this ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping Exception