-->
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: creating new table each time with hibernate
PostPosted: Fri Dec 19, 2008 1:50 am 
Newbie

Joined: Fri Dec 19, 2008 1:41 am
Posts: 1
Location: CHENNAI
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

hi,
iam new to hibernate .
i am creating an new hibernate example


/**
*
*/
package hibernate.oracle.model;

import java.io.Serializable;
import java.sql.SQLException;

import javax.persistence.Entity;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.SequenceGenerator;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.classic.Session;
import com.ddtek.jdbc.extensions.ExtEmbeddedConnection;

/**
* @author Administrator
*
*/
@Entity
public class Users implements Serializable{

private Long id;
private String password;

/**
* @return the id
*/
@Id
@SequenceGenerator(allocationSize=7,initialValue=10,name="cmn_user_seq",sequenceName="USER_SEQ" )
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="cmn_user_seq" )
//@GeneratedValue
public Long getId() {
return id;
}

/**
* @param id the id to set
*/
public void setId(Long id) {
this.id = id;
}

/**
* @return the password
*/
public String getPassword() {
return password;
}

/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}

/**
*
*/
public Users() {
// TODO Auto-generated constructor stub
}

/**
* @param args
*/
public static void main(String[] args) {

try{
AnnotationConfiguration lAnnotationConfigure = new AnnotationConfiguration();
lAnnotationConfigure.addAnnotatedClass(Users.class);
lAnnotationConfigure.configure();

SessionFactory lSessionFactory = lAnnotationConfigure.buildSessionFactory();
System.out.println(lAnnotationConfigure instanceof ExtEmbeddedConnection);
Session lSession = lSessionFactory.getCurrentSession();
lSession.beginTransaction();
Users lUser = new Users();
lUser.setPassword("password");
Users lUser1 = new Users();
lUser1.setPassword("synaptris");
Long id = (Long)lSession.save(lUser);
Long id1 = (Long)lSession.save(lUser1);
System.out.println("THE ID IS "+id);
System.out.println("THE ID OF USER IS "+id1);
lSession.getTransaction().commit();
}
catch(Exception e)
{
e.printStackTrace();
}

}

}

but everytime i run this program

i see i the log information like this

23:26:21,531 INFO SchemaExport:226 - Running hbm2ddl schema export
23:26:21,531 DEBUG SchemaExport:242 - import file not found: /import.sql
23:26:21,531 INFO SchemaExport:251 - exporting generated schema to database
23:26:21,531 DEBUG SchemaExport:377 - drop table Users cascade constraints
23:26:21,640 DEBUG SchemaExport:377 - drop sequence USER_SEQ
23:26:21,656 DEBUG SchemaExport:377 - create table Users (id number(19,0) not null, password varchar2(255 char), primary key (id))
23:26:21,718 DEBUG SchemaExport:377 - create sequence USER_SEQ
23:26:21,718 INFO SchemaExport:268 - schema export complete


i think i drops the table everytime and creates a new one.



The problem is it never creates a new record if i run again the same program.
can anyone help me to solve this.
thank you


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 7:25 am 
Newbie

Joined: Tue Nov 18, 2008 6:08 am
Posts: 14
I am not so familiar with annotations but I know what is happening,
you are setting

Code:
hbm2ddl.auto
criteria to
Code:
create
when it should be set to
Code:
update


so one option is to use hibernate.cfg.xml to configure your hibernate.
or find a way to set it.


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.