-->
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.  [ 4 posts ] 
Author Message
 Post subject: Tables getting deleted...
PostPosted: Wed Apr 16, 2008 1:14 pm 
Newbie

Joined: Tue Mar 18, 2008 11:23 am
Posts: 8
This is gonna be a thick one...

Very short version, and any experts can ask me for more info:

I have a very basic Proof of Concept that I am building. It is using Hibernate JPA Annotations and EntityManagers and I am currently testing my POJO DAOs. But something strange is happening. Whenever I do a simple select in my unit test (using JPQL) with a particular object it deletes the table and rebuilds according to the POJO. I am doing the Proof of Concept on SQL Server 2005, which is where I am having the trouble. If I duplicate the table in MySQL it runs just fine. Info:

For the classes below, I am running on a copy of live data so I've had to obscure the column and table names.

*** The POJO that acts funny ***

@Entity
@Table( name = "problem_table_progress" )
public class PlanProgress implements Serializable{

@Id @GeneratedValue
@Column( name = "problem_table_progress_id" )
private int id;

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

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

@Column
private int step_id;

public int getStep_id(){
return this.Step_id;
}

public void setStep_id(int step_id){
this.step_id = step_id;
}

@Column( name = "USER_ID" )
private int user_id;

public int getUser_id(){
return this.user_id;
}

public void setUser_id(int user_id){
this.user_id = user_id;
}

@Column
private String date_completed;

public String getDate_completed(){
return this.date_completed;
}

public void setDate_completed(String date_completed){
this.date_completed = date_completed;
}

}

*** SQL Server Table Definition (Recreates (according to the POJOs definition) this table whenever I try to select from it) ***

problem_table
problem_table_progress_id (bigint, not null, identity(1,1)
step_id (bigint, not null)
USER_ID (bigint, not null)
date_completed (datetime, not null)

*** MySQL Test Version (Works fine here) ***

problem_table
problem_table_progress_id (bigint, not null, identity(1,1)
step_id (bigint, not null)
USER_ID (bigint, not null)
date_completed (datetime, not null)

*** Working Pojo on same database ***

@Entity
@Table(name = "plan_step")
public class PlanStep implements Serializable{

@Id @GeneratedValue
@Column(name = "plan_step_id")
private int id;

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

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

@Column
private int sort_order;

public int getSort_order(){
return this.sort_order;
}

public void setSort_order(int sort_order){
this.sort_order = sort_order;
}

@Column
private String system_name;

public String getSystem_name(){
return this.system_name;
}

public void setSystem_name(String system_name){
this.system_name = system_name;
}

@Column
private String display_name;

public String getDisplay_name(){
return this.display_name;
}

public void setDisplay_name(String display_name){
this.display_name = display_name;
}

@Column
private String step_status_component_name;

public String getStep_status_component_name(){
return this.step_status_component_name;
}

public void setStep_status_component_name(String step_status_component_name){
this.step_status_component_name = step_status_component_name;
}

@Column
private Boolean allow_access_after_complete;

public Boolean getAllow_access_after_complete(){
return this.allow_access_after_complete;
}

public void setAllow_access_after_complete(Boolean allow_access_after_complete){
this.allow_access_after_complete = allow_access_after_complete;
}

@ManyToOne
@JoinColumn( name = "parent_step_id" )
private ParentStep parent_step;

public ParentStep getParent_step(){
return this.parent_step;
}

public void setParent_step(ParentStep parent_step){
this.parent_step = parent_step;
}

}

*** SQL Server Table Definition ***

parent_step
plan_step_id (bigint, Not Null, Identity(1,1))
sort_order (int, null)
system_name (varchar(50), not null)
display_name (varchar(50), null)
plan_step_status_component_name (varchar(50), not null)
allow_access_after_complete (bit, not null)
parent_step_id (int, null)

*** Persistenece Unit Definition ***

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/ ... ce_1_0.xsd"
version="1.0">

<persistence-unit name="*********">
<provider>org.hibernate.ejb.HibernatePersistence</provider>

<properties>
<property name="hibernate.archive.autodetection" value="class, hbm"/>

<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>

<property name="hibernate.connection.driver_class" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="hibernate.connection.url" value="jdbc:sqlserver://****.***.***.com" />
<property name="hibernate.connection.username" value="*******" />
<property name="hibernate.connection.password" value="*******" />

<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />

<property name="hibernate.hbm2ddl.auto" value="create" />

</properties>

</persistence-unit>

</persistence>

*** Unit Test ***

public void testJPAFactory(){
EntityManager em = PersistenceEngine.getEntityManagerFactory().createEntityManager();

EntityTransaction tx = em.getTransaction();
tx.begin();

List parentSteps = em.createQuery("select pp from PlanProgress as pp").getResultList();

System.out.print("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! : " + parentSteps.size() + " : !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");

tx.commit();
em.close();

assertNotNull(parentSteps);
}


*** Things I've tried ***

I thought it was a column definition thing, so I let it rewrite the table, and then inserted into the table it had just created and ran a unit test selecting again. No dice. It wipes out the one it just created.

I tried several different column definitions, matching exactly the database definition, and still no dice, it wipes the table out.

I tried setting the final transaction action to rollback, just to see if I could keep my old table structure, and it has no affect.

So, I know that is not a lot of info, but I am hoping some experts out there can point me in the right direction. Feel free to ask for any more info you need, save the DB user name and passwords ;)

---L


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 1:43 pm 
Beginner
Beginner

Joined: Fri Aug 24, 2007 4:46 am
Posts: 36
Location: Bielefeld / Germany
Please change
<property name="hibernate.hbm2ddl.auto" value="create" />
to
<property name="hibernate.hbm2ddl.auto" value="update" />
in your config.

"Create" drops / recreates all tables at application startup, "update" changes the schema only.

HTH, Maik


Top
 Profile  
 
 Post subject: Solved?
PostPosted: Wed Apr 16, 2008 2:04 pm 
Newbie

Joined: Tue Mar 18, 2008 11:23 am
Posts: 8
Made the change to "update" in the hbm2ddl and it stopped wiping out the table. Question: where can I get a hold of something that defines what all of the options for hbm2ddl? Also, this may be found in that document, why would it not wipe out all the other tables? Why just this one? Thanks,

---L


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 16, 2008 2:17 pm 
Beginner
Beginner

Joined: Fri Aug 24, 2007 4:46 am
Posts: 36
Location: Bielefeld / Germany
Config-options are best explained here:

http://www.hibernate.org/hib_docs/v3/re ... ation.html

To your other question: Hibernate creates/updates/drops only tables which are "defined" via your mappings.

Cheers, Maik


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