aspose file tools*
The moose likes Object Relational Mapping and the fly likes What's wrong with my code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "What Watch "What New topic
Author

What's wrong with my code?

Rathan Kamireddy
Greenhorn

Joined: Apr 12, 2012
Posts: 5
While I tried to execute my hibernate with oracle xe I got the following error.
Hibernate: insert into USER_DETAILS (data, mydate, phonenumber, USER_NAME, USER_ID) values (?, ?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:184)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
at org.rathan.dto.Test.main(Test.java:31)
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist

at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:367)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:9119)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)



Here is my UserDetails.java

package org.rathan.dto;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table (name="USER_DETAILS")
public class UserDetails {
@Id
@Column (name="USER_ID")
private int userid;
@Column (name="USER_NAME")
private String username;
@Transient
private String Address;
private static long salary;
private String phonenumber;
@Lob
private String data;
@Temporal (TemporalType.DATE)
private Date mydate;

public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}

public Date getMydate() {
return mydate;
}
public void setMydate(Date mydate) {
this.mydate = mydate;
}
public String getAddress() {
return Address;
}
public void setAddress(String address) {
Address = address;
}
public static long getSalary() {
return salary;
}
public static void setSalary(long salary) {
UserDetails.salary = salary;
}
public String getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(String phonenumber) {
this.phonenumber = phonenumber;
}
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}

Here is my Test.java

package org.rathan.dto;

import java.util.Date;

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

public class Test {

public static void main(String[] args) {
System.out.println(1);
UserDetails user=new UserDetails();
System.out.println(2);
user.setUserid(100);
user.setUsername("Rathan");
//user.setSalary(10000);
user.setPhonenumber("9989242487");
user.setAddress("My Home address");
user.setData("blah....blah....blah....blah....blah....");
user.setMydate(new Date());
System.out.println(3);
SessionFactory factory=new Configuration().configure().buildSessionFactory();
System.out.println(4);
Session session=factory.openSession();
System.out.println(5);
session.beginTransaction();
System.out.println(6);
session.save(user);
System.out.println(7);
session.getTransaction().commit();
System.out.println(8);
session.close();
/*user=null;
session=factory.openSession();
session.beginTransaction();
user=(UserDetails)session.get(UserDetails.class, 100);
System.out.println("The Retrieved details are: "+user.getUsername());*/
}
}

I added jdbc14.jar. And all jars from 'required' and 'jpa' folder of hibernate3.6.4 .
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

Rathan Kamireddy wrote:
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist

Can you confirm the presence of the table ?
Also, please try querying without using hibernate (from console/ using jdbc)
Anurag Verma
Ranch Hand

Joined: Mar 30, 2012
Posts: 124

can you provide or verify your database configurations are correct & the database and table exists?
Rathan Kamireddy
Greenhorn

Joined: Apr 12, 2012
Posts: 5
Sumit Bisht wrote:
Rathan Kamireddy wrote:
Caused by: java.sql.BatchUpdateException: ORA-00942: table or view does not exist

Can you confirm the presence of the table ?
Also, please try querying without using hibernate (from console/ using jdbc)


I checked in the oracle database. There is no table with the same name. Even the program couldn't even create the new table dude.. it is lil frustrating...
Rathan Kamireddy
Greenhorn

Joined: Apr 12, 2012
Posts: 5
Anurag Verma wrote:can you provide or verify your database configurations are correct & the database and table exists?


Here are my configurations in xml file...hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory
name="java:hibernate/SessionFactory">
<!-- properties -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbcracle:thin:@localhost:1521:XE</property>
<property name="connection.username">system</property>
<property name="connection.password">srirama</property>
<property name="connection.autocommit">true</property>
<property name="connection.pool_size">1</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<!-- <property name="dialect">org.hibernate.dialect.OracleDialect</property> -->
<property name="show_sql">true</property>
<property name="hbm2.ddl.auto">create</property>
<!-- mapping files -->
<mapping class="org.rathan.dto.UserDetails"/>
</session-factory>
</hibernate-configuration>
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Well of course you will not be able to insert into a table that does not exist.

Also beware that the schema used may default to your own userid, if you do not provide one.

As for the application not being able to create the table, that could be permissions and authorities.

Our enterprise apps can CRUD all day long but cannot Alter or create any DB entities.

WP
Rathan Kamireddy
Greenhorn

Joined: Apr 12, 2012
Posts: 5
William P O'Sullivan wrote:Well of course you will not be able to insert into a table that does not exist.

Also beware that the schema used may default to your own userid, if you do not provide one.

As for the application not being able to create the table, that could be permissions and authorities.

Our enterprise apps can CRUD all day long but cannot Alter or create any DB entities.

WP


Yes, I just manually created the table and executed the program again. The data has been inserted. Great!
But is there any way you can tell me how to give permissions to hibernate to create the table ??
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Great to hear...

You may have to talk to the DBAs or issue GRANTs.

If not, then your hibernate may have a missing parameter.
I see the "create" in your config. Maybe it needs something else.

I personally never create db entities unless in a test/dev/in-memory mode.
Too many problems can occur, and I want to control my DB.

WP
Rathan Kamireddy
Greenhorn

Joined: Apr 12, 2012
Posts: 5
William P O'Sullivan wrote:Great to hear...

You may have to talk to the DBAs or issue GRANTs.

If not, then your hibernate may have a missing parameter.
I see the "create" in your config. Maybe it needs something else.

I personally never create db entities unless in a test/dev/in-memory mode.
Too many problems can occur, and I want to control my DB.

WP


I tried like "<property name="hbm2.ddl.auto">update</property>"
But still the hibernate couldn't create a table by itself.

 
Consider Paul's rocket mass heater.
 
subject: What's wrong with my code?