-->
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.  [ 2 posts ] 
Author Message
 Post subject: insert into table that contains auto increment column
PostPosted: Tue Feb 05, 2008 6:42 am 
Newbie

Joined: Tue Feb 05, 2008 6:24 am
Posts: 3
Hello;


i have a table that contains thrree columns

reviewId | reviewerName |reviewerId

whare reviewId is primary key and auto_increment.

i had POJO class

Code:

package com.ahs.da.hib;

/**
* Review entity.
*
* @author MyEclipse Persistence Tools
*/

public class Review implements java.io.Serializable {

   // Fields

   private Integer reviewId;
   private String reviewName;
   private String reviewerId;

   // Constructors

   /** default constructor */
   public Review() {
   }

   /** full constructor */
   public Review(Integer reviewId, String reviewName, String reviewerId) {
      this.reviewId = reviewId;
      this.reviewName = reviewName;
      this.reviewerId = reviewerId;
   }

   // Property accessors

   public Integer getReviewId() {
      return this.reviewId;
   }

   public void setReviewId(Integer reviewId) {
      this.reviewId = reviewId;
   }

   public String getReviewName() {
      return this.reviewName;
   }

   public void setReviewName(String reviewName) {
      this.reviewName = reviewName;
   }

   public String getReviewerId() {
      return this.reviewerId;
   }

   public void setReviewerId(String reviewerId) {
      this.reviewerId = reviewerId;
   }

}


i have to insert a row into that review table
i am trying to set the reviewerName, reviewerId and saveing the session trying to commit the transaction. it is failing to insert row into table.



mapping file is

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">
<!--
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.ahs.da.hib.Review" table="review" catalog="review">
        <id name="reviewId" type="java.lang.Integer">
            <column name="reviewId" />
            <generator class="assigned" />
        </id>
        <property name="reviewName" type="java.lang.String">
            <column name="reviewName" length="45" not-null="true" />
        </property>
        <property name="reviewerId" type="java.lang.String">
            <column name="reviewerId" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>




Data access code is

Code:

org.hibernate.Session hsession=com.ahs.da.hib.MySQLSF.getSession();
Transaction tx=hsession.beginTransaction();
         
try{
com.ahs.da.hib.Review review =new com.ahs.da.hib.Review();
review.setReviewName(reviewer);
review.setReviewerId(reviewer);   
hsession.save(review);
tx.commit();
}catch(Exception e){
tx.rollback();
return mapping.findForward("failure");
}

what i confused is is there any special way to handle the auto incremented columns. how can i insert the row int to table

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 05, 2008 8:07 am 
Beginner
Beginner

Joined: Mon Jan 07, 2008 4:13 am
Posts: 22
Hi, change your hibernate definition to:
Code:
<hibernate-mapping>
    <class name="com.ahs.da.hib.Review" table="review" catalog="review">
        <id name="reviewId"  name="reviewId" type="java.lang.Integer">
            <generator class="native"/>
        </id>
        <property name="reviewName" type="java.lang.String">
            <column name="reviewName" length="45" not-null="true" />
        </property>
        <property name="reviewerId" type="java.lang.String">
            <column name="reviewerId" length="45" not-null="true" />
        </property>
    </class>
</hibernate-mapping>


Ivan


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