hi,
i would like to create a modell that reads and writes to
multiple schemas in mysql. the clou on it is
that i have to classes Foo and Bar that reside in
different schemas (in mysql terms databases);
where Foo has a reference on Bar
first the java code
Code:
public class Foo
{ ...
public String getId(){return id_;}
public Foo setId(String _id)
{id_ = _id;return this;}
public Bar getBar(){return bar_;}
public Foo setBar(Bar _value)
{bar_=_value;return this;}
public String getName(){return name_;}
public Foo setName(String _value)
{name_ = _value;return this;}}
public class Bar
{ ...
public String getId(){return id_;}
public Bar setId(String _id)
{id_ = _id;return this;}
public String getName(){return name_;}
public Bar setName(String _value)
{name_ = _value;return this;}}
}
now here the database
Code:
create database A;
use A;
create table FOO
( ID CHAR(32) NOT NULL PRIMARY KEY,
NAME VARCHAR(50) NULL ,
BAR_ID CHAR(32) NOT NULL,
) TYPE = INNODB;
create database B;
use B;
create table BAR
( ID CHAR(32) NOT NULL PRIMARY KEY,
NAME VARCHAR(50) NULL ,
) TYPE = INNODB;
now the mappings
Code:
<hibernate-mapping>
<class name="org.pragmatico.Foo"
table="FOO" schema="A"
dynamic-update="false" dynamic-insert="false">
<id name="id" column="id" type="java.lang.String">
<generator class="uuid.hex"/>
</id>
<many-to-one name="bar"
class="org.pragmatico.Bar" cascade="none"
outer-join="auto" update="true"
insert="true" column="MERCHANT_ID"/>
<property name="name"
type="java.lang.String" update="true"
insert="true" column="name"/>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class
name="org.pragmatico.Bar" table="BAR"
schema="B" dynamic-update="false"
dynamic-insert="false">
<id name="id" column="id" type="java.lang.String">
<generator class="uuid.hex"/>
</id>
<property name="name"
type="java.lang.String" update="true"
insert="true" column="name"/>
</class>
</hibernate-mapping>
and finally the hibernatexml.cfg
Code:
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver</property>
<property
name="hibernate.connection.url">
jdbc:mysql://localhost/</property>
<property
name="hibernate.dialect">
net.sf.hibernate.dialect.MySQLDialect</property>
<mapping
resource="org/pragmatico/Foo.hbm.xml"/>
<mapping
resource="org/pragmatico/Bar.hbm.xml"/>
</session-factory>
</hibernate-configuration>
now for the love of god i cant figure out how to get that
running. i tried to put the name of the database to the
connection.url i added and removed the schema-attr
on the mappings i did this i did that. i simply dont know
what to do. maybe its not possible.
what i want to do boils down to this
Code:
Foo foo = new Foo();
foo.setName("FOO 01");
Bar bar = new Bar();
bar.setName("BAR 01");
foo.setBar(bar);
Configuration conf = new Configuration();
conf.configure();
SessionFactory sessionFactory =
conf.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
session.save(foo);
transaction.commit();
session.close();
any ideas.
thank you
ciao robertj