Two Laptop Bag
The moose likes Object Relational Mapping and the fly likes Data deleted while getting data from table using hibernate. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply Bookmark "Data deleted while getting data from table using hibernate." Watch "Data deleted while getting data from table using hibernate." New topic
Author

Data deleted while getting data from table using hibernate.

prashantk patil
Greenhorn

Joined: Dec 21, 2008
Posts: 3
I have a problem while retrieving data from User table.

Mapping in User.hbm.xml like this ::

<hibernate-mapping>
<class name="com.mcardio.entity.User" table="user" dynamic-update="true" catalog="mcardio">
<comment></comment>
<id name="userId" type="string" unsaved-value="sensible" length="30">
<column name="USER_ID" />
</id>
<property name="password" type="string" length="30">
<column name="PASSWORD">
<comment></comment>
</column>
</property>
......
</class>
</hibernate-mapping>

and I try to get data like this ::

User user = (User)currentSession.get(User.class, userId);

But when I try this already added data deleted by the hibernate.
How can I get the data without deleting it is there any mapping problem?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Hibernate doesn't delete data as part of a get. Are you using the schema creation stuff in your SessionFactory configuration?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
prashantk patil
Greenhorn

Joined: Dec 21, 2008
Posts: 3
No sir I am not using any shema my hibernate configuration is like this

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<!-- MySQL db connection -->
<property name="connection.url">jdbc:mysql://localhost:3306/mcardio</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.password">root</property>

<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>

<property name="current_session_context_class">thread</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="hibernate/Patient.hbm.xml" />
<mapping resource="hibernate/Device.hbm.xml" />
<mapping resource="hibernate/PatientDevices.hbm.xml" />
<mapping resource="hibernate/User.hbm.xml" />
</session-factory>

</hibernate-configuration>

And Session is created such as

private static String HIBERNATE_CONFIG_LOCATION = "hibernate/mcardioHibernate.cfg.xml";

private static final Configuration cfg = new Configuration();

And fetch by using code

tx = currentSession.beginTransaction();
//User temp = new User(userId,"doctor","DOCTOR",new Integer(1));
//currentSession.save(temp);
User user = (User)currentSession.get(User.class, userId);
tx.commit();

If I use a commented code above I can save and retrieve it but when I restart tomcat and commenting code for save, the added data automatically deleted.


[ December 22, 2008: Message edited by: prashantk patil ]
[ December 22, 2008: Message edited by: prashantk patil ]
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


No sir I am not using any shema my hibernate configuration is like this

I was meaning Hibernate's schema creation functionality (not an XML schema), and yes you are using it:

This will drop and recreate the schema everytime the SessionFactory is started.
prashantk patil
Greenhorn

Joined: Dec 21, 2008
Posts: 3
sorry and thank you very much.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by prashantk patil:
sorry and thank you very much.


No need to apologize ! Glad you got it sorted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Data deleted while getting data from table using hibernate.
 
Similar Threads
Hibernate Association
Association references unmapped class exception with hibernate
Many to Many mapping in hibernate.
Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.
Hibernate Association