• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

simple hibernate excercise

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys

I was doing a small example on hibernate, but i am stuck.

From my DAOImplimentation class i am calling Hibernate and i just want to do a simple insert using an sql statement.

This is my DaoImplimentation class where i have the query

public class TestDaoImpl implements TestDao
{

public TestReturnVO getInfo(String ssn) throws DaoException;

public TestReturnVO insertInfo(TestVO testVO)
{
System.out.println("Inside Insert ssn");
TestReturnVO trVO = null;
//Call hibernate here.

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// Beginning of SQL test, its db2 database
SQLQuery sqlQuery = session.createSQLQuery("insert into JAY.GENERALINFO('rani','london','3125552956', '1980-01-09','11')");
session.getTransaction().commit();

System.out.println("Inserted info record done");

return trVO;
}

}

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

This is my hibernate.cfg.xml file

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->
<property name="connection.datasource">frameworkAppDb</property>
<property name="show_sql">false</property>
<property name="dialect">org.hibernate.dialect.DB2390Dialect</property>
<property name="current_session_context_class">thread</property>
<mapping resource="spResourceMapping/TestVO.hbm.xml"/>

</session-factory>

</hibernate-configuration>

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
and this is TestVO.hbm.xml file

<hibernate-mapping>

<class name="com.aoc.judiciary.framework.ValueObjects.TestVO">

<property name="name" column="1"/>
<property name="city" column="2"/>
<property name="ssn" column="3"/>
<property name="dob" column="4"/>
<property name="userId" column="5"/>


</class>

</hibernate-mapping>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++

and this is the error i get

++++++++++++++++++++++++++++++++++++++++++++++
[5/14/07 2:05:06:557 GMT] 00000034 Configuration I Reading mappings from resource: spResourceMapping/TestVO.hbm.xml
[5/14/07 2:05:06:589 GMT] 00000034 XMLHelper E Error parsing XML: XML InputStream(17) 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)*)".
++++++++++++++++++++++++++++++++++++++++++++++

am i missing something?

Also, "userId" (column=5, in TestVO.hbm.xml) is the primary key i have in the database. so Do i have to define it as primary key in my TestVO.hbm.xml file or is it optional?

any solutions greatly appreciated,
thanks very much
Jay
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jay,


Have you defined a pk for your TestVO?

Regards.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
he's right your hibernate mapping file is missing an id mapping (id or composite-id) map to the PK on your table.
 
Nick Williamson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ps if you're going to use hibernate you mid-as-well use it... instead of writing sql from you session to insert, create an object (one of type of the mapping file) and set the values on the object and then call save from the session object

i.e.

session.save(myBean);
 
jay roy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks guys

yes, i defined an id like this
<property name="name" column="1"/>
<property name="city" column="2"/>

<property name="ssn" column="3"/>
<property name="dob" column="4"/>
<id name="userId" column="5">
<generator class="native"/>
</id>

can anyone tell me what this <id..> represents?

If i have a composite key in my database (or multiple primary keys)
how should my above xml change?

for example if ssn and userId are my composite key, then what do i do?

any responces greatly appreciated

thanks
J
 
Nick Williamson
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rtfm

mappings:
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#tutorial-associations-mappinguser

composit id:
http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#mapping-declaration-compositeid

it's all right there on one page for you.

I've had almost all my problems solved between the link I gave you and the source code. Good luck with hibernate.
 
jay roy
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks everyone
 
reply
    Bookmark Topic Watch Topic
  • New Topic