Forums Register Login

Mapping Exception

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 


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.
+Pie Number of slices to send: Send
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 .
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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 ?
Hot dog! An advertiser loves us THIS much:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6539 times.
Similar Threads
Hibernate 3.0 many-to-one relationships
One to many Relation
One to Many && Many to One mapping
Problem with Hibernate test application
Error: "Could not synchronize database state with session"
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 16:01:17.