-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 
Author Message
 Post subject: Hibernate Mapping Trouble - Please Help
PostPosted: Fri Aug 22, 2008 9:36 am 
Newbie

Joined: Tue Dec 20, 2005 5:47 pm
Posts: 13
I have table which does not have a primary key however it has column which is unique. my question is how do i write the mapping document for this type of sort of tables? any how here is my sql statement for the table:

Code:
create table test(
test_id varchar(210),
message varchar(210)
);

insert into test(test_id, message)values(1, 'aertaer');

insert into test(test_id, message)values(2, 'traraer');



Please note 'test_id' is unique.

Many thanks

Hibernate version:3

Mapping documents:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="testhibernate.Test" table="Test">
<property name="TestId" column="test_id" type="java.lang.String" unique="true"/>
<property name="Message" column="message" type="java.lang.String" />
</class>
</hibernate-mapping>

Full stack trace of any exception that occurs:
SEVERE: Error parsing XML: XML InputStream(8) The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".
Exception in thread "main" java.lang.ExceptionInInitializerError
at testhibernate.Main.main(Main.java:27)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from resource testhibernate/test.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
at testhibernate.DAO.<clinit>(DAO.java:22)
... 1 more
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:502)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
... 8 more
Name and version of the database you are using:
MySQL4


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 10:01 am 
Newbie

Joined: Mon Jul 07, 2008 12:31 pm
Posts: 5
The unique column is the de facto key...

The XML parsing error indicates that you must specify <id> or <composite-id>. Map the test_id as the id with an assigned identifier rather than as a property.

Code:
      <id name="TestId" column="test_id" type="java.lang.String">
         <generator class="assigned"/>
      </id>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 10:08 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
The hibernate mapping required de id ( primary key ). Set de test_id to id.

Code:
<id name="NameOfThisPropertyInYourClass" column="test_id" type="string">
   <generator class="assigned"/>
</id>


For more information please read mapping-declaration-id

_________________
Please don't forget to rateME!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 11:38 am 
Newbie

Joined: Tue Dec 20, 2005 5:47 pm
Posts: 13
Thanks you guys for the help. i think i have done what you guys have suggested however on this occassion it has not worked. if i'm missing the point or if you have any more ideas please let me know.

thanks once more.

here is my new xml document:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
    <class name="testhibernate.Test" table="Test">
        <id name="TestId" column="test_id" type="string">
            <generator class="assigned" />
        </id>
        <property name="Message" column="message" type="string" />
    </class>
</hibernate-mapping>


here is my DAO classes
Code:
package testhibernate;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

public class TestDAO extends DAO {
    public Test getByTestId(String test_id) throws Exception{
        try {
            begin();
            Query q = getSession().createQuery("from Test where test_id = :test_id");
            q.setString("test_id", test_id);
            Test test = (Test)q.uniqueResult();
            commit();
            return test;
        }catch(Exception ex) {
            rollback();
            throw new Exception("Some thing went wrong here!");
        }
    }
   
    public void save(Test arg) {
        try {
            begin();
            getSession().save(arg);
            commit();
        }catch(HibernateException hex) {
            rollback();
            hex.printStackTrace();
        }
    }

    public void update(Test arg) throws Exception {
        try {
            begin();
            getSession().update(arg);
            commit();
        }catch(HibernateException hex) {
            rollback();
            throw new Exception("Some thing went wrong here!");
        }
    }

    public void delete(Test arg) throws Exception{
        try {
            begin();
            getSession().delete(arg);
            commit();
        }catch(Exception ex) {
            rollback();
            throw new Exception("Some thing went wrong here!");
        }
    }

    public List list() throws Exception {
        try{
            begin();
            Query q = getSession().createQuery("from Test");
            List list = q.list();
            return list;
        }catch(Exception ex){
            rollback();
            throw new Exception("Some thing went wrong here!");
        }
    }
}


here is my table class
Code:
package testhibernate;

public class Test {
    private String test_id;
    private String message;

    public String getTestId() {
        return this.test_id;
    }

    public void setTestId(String arg) {
        this.test_id = arg;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(String arg) {
        this.message = arg;
    }
}


Code:
package testhibernate;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class DAO {
    private static final Logger log = Logger.getAnonymousLogger();
    private static final ThreadLocal session  = new ThreadLocal();
    private static final SessionFactory sessionFactory =
            new Configuration().configure().buildSessionFactory();

    protected DAO(){}

    public static Session getSession() {
        Session session = (Session) DAO.session.get();
        if(session == null){
            session = sessionFactory.openSession();
            DAO.session.set(session);
        }
        return session;
    }

    public void begin() {
        getSession().beginTransaction();
    }

    public void commit() {
        getSession().getTransaction().commit();
    }

    public void rollback() {
        try{
            getSession().getTransaction().rollback();
        }catch(HibernateException ex) {
            log.log(Level.WARNING, "Cannot rollback", ex);
        }

        try{
            getSession().close();
        }catch(HibernateException ex){
            log.log(Level.WARNING, "Cannot close", ex);
        }
        DAO.session.set(null);
    }

    public static void close() {
        getSession().close();
        DAO.session.set(null);
    }
}



and finally here is my error code
Code:
Aug 22, 2008 4:28:18 PM org.hibernate.impl.SessionFactoryImpl <init>
INFO: building session factory
Aug 22, 2008 4:28:18 PM org.hibernate.impl.SessionFactoryObjectFactory addInstance
INFO: Not binding factory to JNDI, no JNDI name configured
Hibernate: select test0_.test_id as test1_0_, test0_.message as message0_ from Test test0_
Aug 22, 2008 4:28:18 PM org.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 1054, SQLState: 42S22
Aug 22, 2008 4:28:18 PM org.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: Unknown column 'test0_.test_id' in 'field list'
java.lang.Exception: Some thing went wrong here!
        at testhibernate.TestDAO.list(TestDAO.java:72)
        at testhibernate.Main.main(Main.java:27)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 11:53 am 
Newbie

Joined: Mon Jul 07, 2008 12:31 pm
Posts: 5
Do you get the same error if you run this directly in an SQL console app:

Code:
select test0_.test_id as test1_0_, test0_.message as message0_ from Test test0_


Does the table "test" definitely have a column "test_id"? It seems to be saying that it doesn't!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 12:02 pm 
Regular
Regular

Joined: Mon Apr 19, 2004 6:54 pm
Posts: 79
Try one of these:

Code:
from Test t where t.testId = :test_id


or

Code:
from Test t where t.id = :test_id


I think since you have gettter/setter getTestId/getTestId, you should use testId for the property name in you hql.

Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 12:14 pm 
Newbie

Joined: Tue Dec 20, 2005 5:47 pm
Posts: 13
na! no luck however here is my main class if that is any help. the error is thrown in TestDAO class @ method list()

Code:
package testhibernate;


import java.util.Iterator;
import java.util.List;



public class Main {

    public static void main(String[] args) {
        try{
            List<Test> tests = new TestDAO().list();
            Iterator ti = tests.iterator();
            while(ti.hasNext()) {
                Test test = (Test)ti.next();
                System.out.println("Test: " + test.getMessage());
                System.out.println();
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
       
    }

}



Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 12:30 pm 
Regular
Regular

Joined: Mon Apr 19, 2004 6:54 pm
Posts: 79
You class name is testhibernate.Test
try:
Code:
from testhibernate.Test t where t.id = : testId


Christophe


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 22, 2008 12:33 pm 
Newbie

Joined: Tue Dec 20, 2005 5:47 pm
Posts: 13
eureka! my column on the test table is test_id_string instead name test_id.

oopsie!

thanks.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.