-->
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.  [ 5 posts ] 
Author Message
 Post subject: I'm a beginner and need help
PostPosted: Fri Sep 09, 2005 11:25 am 
Newbie

Joined: Thu Jul 07, 2005 6:59 am
Posts: 13
Location: Derry Northern Ireland
I have a simple Object I am trying to persist to a simple table.

This is my DDL,

CREATE TABLE "message"
(
"message_id" integer NOT NULL DEFAULT autoincrement,
"message_text" varchar(50) NOT NULL,
PRIMARY KEY ("message_id"),
)

this is my mapping file named message.hbm.xml,

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
   
   <hibernate-mapping>
      <class name="com.duneorbit.test.Message" table="message">
         <id name="id" column="message_id">
            <generator class="increment"/>
         </id>
         <property name="text" column="message_text"/>
      </class>
   </hibernate-mapping>


this is my domain object named Message.java,

Code:
package com.duneorbit.test;

public class Message {
   
   private int messageId;
   private String messageText;
   
   public Message(){}
   
   public Message(int messageId, String messageText){
      this.messageId=messageId;
      this.messageText=messageText;
   }
   
   public int getId(){
      return this.messageId;
   }
   
   public void setId(int messageId){
      this.messageId=messageId;
   }
   
   public String getText(){
      return this.messageText;
   }
   
   public void setText(String messageText){
      this.messageText=messageText;
   }
   
}


this is my hibernate.cfg.xml file,

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd">
   
   <hibernate-configuration>
      <!--properties-->
      <session-factory>
         <property name="connection.driver_class">com.sybase.jdbc2.jdbc.SybDriver</property>
         <property name="dialect">net.sf.hibernate.dialect.SybaseAywhereDialect</property>
         <property name="connection.url">jdbc:sybase:Tds:localhost:2638?ServiceName=stuffed</property>
         <property name="connection.username">DBA</property>
         <property name="connection.password">SQL</property>
         <property name="show_sql">true</property>
         <property name="use_outer_join">true</property>
      
         <!--mapping files-->
         <mapping resource='com/duneorbit/test/message.hbm.xml'/>
      </session-factory>
   </hibernate-configuration>


I have a simple HibernateUtil helper class,

Code:
package com.duneorbit.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;

public class HibernateUtil {
   
   private static Log log = LogFactory.getLog(HibernateUtil.class);
   private static final SessionFactory sessionFactory;
   
   static {
      
      Configuration cfg = new Configuration();
      try{
         sessionFactory = cfg.configure().buildSessionFactory();
      }catch(Throwable ex){
         log.error(ex);
         throw new ExceptionInInitializerError(ex);
      }
      
   }
   
   public static Session getSession() throws HibernateException{
      return sessionFactory.openSession();
   }
   
}


I run my simple junit test method,

Code:
public void testMessageObject(){
      Session session = null;
      try{
         log.info("Starting HibernateUtil class");
         session = HibernateUtil.getSession();
         Transaction tx = session.beginTransaction();
         Message message = new Message(1,"Test");
         session.save(message);
         tx.commit();
         log.info("Done");
      }catch(HibernateException he){
         log.error(he);
         fail("Test Failed");
      }
   }


and this error message

09-Sep-2005 15:02:29 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 102, SQLState: 42W04
09-Sep-2005 15:02:29 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: ASA Error -131: Syntax error near 'message' on line 1
09-Sep-2005 15:02:29 test.com.duneorbit.TestMessage testMessageObject
SEVERE: net.sf.hibernate.exception.SQLGrammarException: Could not save object

can anyone help me, I need help.

Thanks inadvance.


Top
 Profile  
 
 Post subject: text
PostPosted: Fri Sep 09, 2005 11:53 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I suspect that the problem caused by your field name: 'text' is Sybase data type and as H generates something like select _t.message as text .. it confuses Sybase.

1. Please post the actual SQL (should see it in H log when show_sql=true);
2. Try rename field to something like 'txt', or 'body' and see if problem will persist

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 11:53 am 
Newbie

Joined: Fri Jan 07, 2005 7:44 am
Posts: 2
You need to set the Text by calling setText() in the mapping object.

Also you do not need to set the id value as you have set the generator class to "increment" in your hbm file. (i assume you have set the id column to auto increment on your database table?)

try this:

Code:
public void testMessageObject(){
      Session session = null;
      try{
         log.info("Starting HibernateUtil class");
         session = HibernateUtil.getSession();
         Transaction tx = session.beginTransaction();


         //Message message = new Message(1,"Test");
         
          Message message = new Message();
          message.setText("Test");

         session.save(message);
         tx.commit();
         log.info("Done");
      }catch(HibernateException he){
         log.error(he);
         fail("Test Failed");
      }
   }


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 09, 2005 12:07 pm 
Newbie

Joined: Thu Jul 07, 2005 6:59 am
Posts: 13
Location: Derry Northern Ireland
I tried both suggestions here but I get the same error msg.
Where can I find the log file?


Top
 Profile  
 
 Post subject: log4j
PostPosted: Fri Sep 09, 2005 12:34 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
where is your log file is defined by your log4j configuration, try this log4j.properties (make sure it is first log4j configuration file accessible by your classpath)
Code:
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=INFO
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

It should print log on console

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.