-->
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.  [ 8 posts ] 
Author Message
 Post subject: Save record when table do not have primary key
PostPosted: Mon May 28, 2007 2:40 am 
Newbie

Joined: Mon May 28, 2007 2:25 am
Posts: 9
Hi All,

I am facing the problem in the Saving the record here in my table i have not use the primary key....
my code is

<hibernate-mapping>
<class name="com.solversa.test.TbAddress" table="tb_address" catalog="testhibernate">
<composite-id name="id" class="com.solversa.test.TbAddressId">
<key-property name="addId" type="java.lang.Integer">
<column name="addID" />
</key-property>
<key-property name="address" type="java.lang.String">
<column name="Address" />
</key-property>
<key-property name="addressIndex" type="java.lang.Integer">
<column name="address_index" />
</key-property>
<key-property name="userId" type="java.lang.Integer">
<column name="userId" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>

it will create two POJO Classes for the same that is

1:)testMapTest.java

public class TbAddress implements java.io.Serializable {


// Fields

private TbAddressId id;


// Constructors

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




// Property accessors

public TbAddressId getId() {
return this.id;
}

public void setId(TbAddressId id) {
this.id = id;
}

and

TbAddressId.java

public class TbAddressId implements java.io.Serializable {


// Fields

private Integer addId;
private String address;
private Integer addressIndex;
private Integer userId;


// Constructors

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


/** full constructor */
public TbAddressId(Integer addId, String address, Integer addressIndex, Integer userId) {
this.addId = addId;
this.address = address;
this.addressIndex = addressIndex;
this.userId = userId;
}


// Property accessors

public Integer getAddId() {
return this.addId;
}

public void setAddId(Integer addId) {
this.addId = addId;
}

public String getAddress() {
return this.address;
}

public void setAddress(String address) {
this.address = address;
}

public Integer getAddressIndex() {
return this.addressIndex;
}

public void setAddressIndex(Integer addressIndex) {
this.addressIndex = addressIndex;
}

public Integer getUserId() {
return this.userId;
}

public void setUserId(Integer userId) {
this.userId = userId;
}

and Code for the Inserting the Record is :

org.hibernate.Session session=null;
session= HibernateSessionFactory.getSession();
//Get the Record of the Particular User
//Create the Object the Transaction
org.hibernate.Transaction tx;
//Begin the Transaction
tx=session.beginTransaction();

TbAddressId td=new TbAddressId(10,"Pun00e",5,10);
TbAddress address = new TbAddress();
address.setId(td);

//session.save(td);
session.save(address);

tx.commit();
session.flush();
//close the Session
session.close();
but i am getting this errors:

org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:162)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:271)
at org.hibernate.event.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:24)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:719)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:84)
at com.solversa.action.login.execute(login.java:49)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:667)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Unknown Source)
Caused by: java.sql.BatchUpdateException: Table 'testhibernate.testhibernate__tb_address' doesn't exist
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1103)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:853)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:155)
... 27 more

Pls Help me



Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 3:01 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Hi,

for me it seem that there is no problem with the primary key but a problem with the mapping to the table tb_addresss:

Code:
Caused by: java.sql.BatchUpdateException: Table 'testhibernate.testhibernate__tb_address' doesn't exist


I think you should have a look in your mapping file. I am no expert, but what happens if you delete catalog="testhibernate"?

Regards,
TMK


Top
 Profile  
 
 Post subject: Hi
PostPosted: Mon May 28, 2007 4:29 am 
Newbie

Joined: Mon May 28, 2007 2:25 am
Posts: 9
Hi tmk ,

Thnaks a lot my problem is get solved but pls tell me is it right way to doing this....

Can we always need to pass the addID?

Can we set as auto increament with setting it Primary key....as we define is as compositeID as above...

Thanks,

Ashish

tmk wrote:
Hi,

for me it seem that there is no problem with the primary key but a problem with the mapping to the table tb_addresss:

Code:
Caused by: java.sql.BatchUpdateException: Table 'testhibernate.testhibernate__tb_address' doesn't exist


I think you should have a look in your mapping file. I am no expert, but what happens if you delete catalog="testhibernate"?

Regards,
TMK


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 5:08 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
When you want to use auto increment, then you should use a primary key in your database.
You need to configure your mapping file like this for your id column which should use auto increment:

<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>

Please rate me if I could help you :)


Top
 Profile  
 
 Post subject: Hi
PostPosted: Mon May 28, 2007 5:26 am 
Newbie

Joined: Mon May 28, 2007 2:25 am
Posts: 9
Hi,

In my table there is no primary key...

as every time we need to pass the ID record also.....

or is there any other way to do this.......

tmk wrote:
When you want to use auto increment, then you should use a primary key in your database.
You need to configure your mapping file like this for your id column which should use auto increment:

<id name="id" column="EVENT_ID">
<generator class="native"/>
</id>

Please rate me if I could help you :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 6:34 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Why don't you define a primary key? If you cannot use an existing column, you can add a new one only for the auto increment purpose...


Top
 Profile  
 
 Post subject: Hi
PostPosted: Mon May 28, 2007 6:57 am 
Newbie

Joined: Mon May 28, 2007 2:25 am
Posts: 9
In My Project Database We are creating the CompositeID as

<composite-id name="id" class="com.MYname.test.TbAddressId">
<key-property name="addId" type="java.lang.Integer">
<column name="addID" />
</key-property>
<key-property name="address" type="java.lang.String">
<column name="Address" />
</key-property>
<key-property name="addressIndex" type="java.lang.Integer">
<column name="address_index" />
</key-property>
<key-property name="userId" type="java.lang.Integer">
<column name="userId" />
</key-property>
</composite-id>

Actually i am new in Hibernate ......

How we can perform CRUD(Insert,Delete,update,select) Opeartion in Composite-id ?

Can you pls give me the Sample code for the Same.

Thnaks,
Ashish



tmk wrote:
Why don't you define a primary key? If you cannot use an existing column, you can add a new one only for the auto increment purpose...


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 28, 2007 8:23 am 
Beginner
Beginner

Joined: Thu Mar 29, 2007 11:57 am
Posts: 27
Sorry I have never used composite-id with hibernate. Perhaps someone else can help you... or the docs.


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