Dear All,
I am using hibernate SchemaExport to generate the create, drop and alter scripts but I am not getting the alter table scripts. Below are the files I am using to generate the DDL script.
Employee.java -------------- @Entity @Table(name = "EMPLOYEE") public class Employee implements java.io.Serializable {
private static final long serialVersionUID = 1L;
private long empId; private String empName; private boolean manager; public Employee() { }
public Employee(String empName) { this.empName = empName; }
@Id @GeneratedValue @Column(name = "EMP_ID") public long getEmpId() { return this.empId; }
public void setEmpId(long empId) { this.empId = empId; }
@Column(name = "EMP_NAME", nullable = false, length = 100) public String getEmpName() { return this.empName; }
public void setEmpName(String empName) { this.empName = empName; }
@Column(name = "MANAGER", nullable = false, length = 1) @org.hibernate.annotations.Check(constraints = "manager=true") public boolean isManager() { return manager; }
public void setManager(boolean manager) { this.manager = manager; }
}
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> <session-factory name=""> <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="hibernate.connection.url">jdbc:oracle:thin:@172.25.108.238:1521:dlangebotsys</property> <property name="hibernate.connection.username">dlangebot</property> <property name="hibernate.connection.password">dlangebot</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <!-- <property name="hibernate.hbm2ddl.create">update</property> --> <property name="hibernate.hbm2ddl.auto">create-update</property> <!-- <property name="hibernate.hbm2ddl.auto">update</property> --> <!-- Mapping files --> <mapping class="com.hw.Employee" /> </session-factory> </hibernate-configuration>
HibernateUtil.java ------------------- public class HibernateUtil {
private static final SessionFactory sessionFactory;
static { try { File file = new File("D:/DdlQueryFile.sql"); if (!file.exists()) { file.createNewFile(); System.out.println("File Created"); }
AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration(); annotationConfiguration = annotationConfiguration.configure(); SchemaExport schemaExport = new SchemaExport( annotationConfiguration); schemaExport.setOutputFile("D:/" + file.getName()); /* * Exports the * script to the * given file */ schemaExport.setFormat(true); /* Formats the created script */ schemaExport.setDelimiter(";"); /* * Adds delimeter after every DDL * script */ schemaExport.execute(true /* script */, true /* export to db */, false /* just drop */, true /* just create */);
@SuppressWarnings("unchecked") List<HibernateException> schemaExportExceptionList = schemaExport .getExceptions(); if (schemaExportExceptionList != null) { for (Iterator<HibernateException> iterator = schemaExportExceptionList .iterator(); iterator.hasNext();) { HibernateException hibernateException = iterator.next(); hibernateException.printStackTrace(); } } sessionFactory = annotationConfiguration.buildSessionFactory();
} catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
public static SessionFactory getSessionFactory() { return sessionFactory; } }
Generated Script ---------------- create table EMPLOYEE ( EMP_ID number(19,0) not null, EMP_NAME varchar2(100 char) not null, MANAGER number(1,0) not null, primary key (EMP_ID) );
Desired -------- If table is existing in database and corresponding mapping file is changed (Employee.java in our case) I want only a alter table script should be generated adding the new column (empAdd in our case).
PROBLEM -------- When I am adding the below code to Employee.java and executing it.
private String empAdd;
@Column(name = "EMP_ADD", nullable = false, length = 100) public String getEmpAdd() { return empAdd; }
public void setEmpAdd(String empAdd) { this.empAdd = empAdd; }
Instead of generating alter script for the existing employee table it is throwing the following exception.
create table EMPLOYEE ( EMP_ID number(19,0) not null, EMP_ADD varchar2(100 char) not null, EMP_NAME varchar2(100 char) not null, MANAGER number(1,0) not null, primary key (EMP_ID) ); Apr 27, 2011 10:21:10 AM org.hibernate.tool.hbm2ddl.SchemaExport create SEVERE: Unsuccessful: create table EMPLOYEE (EMP_ID number(19,0) not null, EMP_ADD varchar2(100 char) not null, EMP_NAME varchar2(100 char) not null, MANAGER number(1,0) not null, primary key (EMP_ID)) Apr 27, 2011 10:21:10 AM org.hibernate.tool.hbm2ddl.SchemaExport create SEVERE: ORA-00955: name is already used by an existing object
Could you please let me know where am I making mistake or kindly guide me to achieve the same.
_________________ Vinay
|