Quote:
If I execute the source code that I've posted it uses the default value (map) and not testschema.
How do you know? Is it in the generated SQL?
I've built a test case which seems to work perfectly.
Here's the generate ddl when I _don't_ manipulate the schema in java:
Code:
11:23:09,453 INFO SchemaExport:36 - Running hbm2ddl schema export
drop table if exists map.CHA20P
create table map.CHA20P (id bigint not null auto_increment, data varchar(255), primary key (id))
11:23:09,453 INFO SchemaExport:36 - schema export complete
Hibernate: insert into map.CHA20P (data) values (?)
11:23:09,546 WARN JDBCExceptionReporter:66 - SQL Error: 1146, SQLState: 42S02
11:23:09,546 ERROR JDBCExceptionReporter:24 - Table 'map.cha20p' doesn't exist
As you can see it fails because its using map.chap20p and map doesn't exist in my db.
If I include the rootClass.getTable().setSchema("test") I get the following:
Code:
11:26:43,859 INFO SchemaExport:36 - Running hbm2ddl schema export
drop table if exists test.CHA20P
create table test.CHA20P (id bigint not null auto_increment, data varchar(255), primary key (id))
11:26:43,859 INFO SchemaExport:36 - schema export complete
Hibernate: insert into test.CHA20P (data) values (?)
Hibernate: select charge0_.id as id0_0_, charge0_.data as data0_0_ from test.CHA20P charge0_ where charge0_.id=?
Hibernate: update test.CHA20P set data=? where id=?
No problem here as "test" is a valid schema.
Here's the code, etc. perhaps it'll be of some help. btw, I'm using hibernate v3.2.5ga.
Charge.java
Code:
package test.renameschema;
public class Charge {
private Long id;
private String data;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}
Charge.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="test.renameschema">
<class name="Charge" table="CHA20P" schema="map">
<id name="id">
<generator class="native"/>
</id>
<property name="data"/>
</class>
</hibernate-mapping>
Test.java
Code:
package test.renameschema;
import junit.framework.TestCase;
import org.hibernate.Session;
public class Test extends TestCase {
public void testUpdate() {
createCharge();
Session s = HibernateUtil.getSession();
s.beginTransaction();
Charge charge = (Charge)s.load(Charge.class, new Long(1));
charge.setData("data updated");
s.update(charge);
s.getTransaction().commit();
s.close();
}
private void createCharge() {
Session s = HibernateUtil.getSession();
s.beginTransaction();
Charge charge = new Charge();
charge.setData("data");
s.save(charge);
s.getTransaction().commit();
s.close();
}
}
HibernateUtil.java
Code:
package test.renameschema;
import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.mapping.RootClass;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateUtil {
private static SessionFactory factory;
static {
Configuration configuration;
try{
configuration = new Configuration().configure("/hibernate.cfg.xml");
Iterator iterator = configuration.getClassMappings();
while (iterator.hasNext()) {
RootClass rootClass = (RootClass)iterator.next();
System.out.println("&&&&: "+rootClass.getTable().getName());
if (rootClass.getTable().getName().equals("CHA20P")){
rootClass.getTable().setSchema("test");
setSessionFactory(configuration.buildSessionFactory());
break;
}
}
}
catch(Throwable ex){
throw new ExceptionInInitializerError(ex);
}
// Print generated schema to console
new SchemaExport(configuration).create(true,false);
}
public static synchronized Session getSession() {
if (factory == null) {
factory = new Configuration().configure().buildSessionFactory();
}
return factory.openSession();
}
public static void setSessionFactory(SessionFactory factory) {
HibernateUtil.factory = factory;
}
}
hibernate.cfg.xml
Code:
<?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">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create-drop</property>
<mapping resource="test/renameschema/Charge.hbm.xml"/>
</session-factory>
</hibernate-configuration>