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