The Device object can be persisted before.Now to meet the user's needs,we want to create a new class CompositeDevice which extends Device as below:
package com.omnet.test;
public class CompositeDevice extends Device
{
private Device _host;
private int _slot;
public int getSlot() { return _slot; }
public void setSlot(int slot) { _slot = slot; }
public Device getHost() { return _host; }
public void setHost(Device device) { _host = device; }
}
Device.hbm.xml
<class name="com.omnet.test.Device" table="device">
<id name="devId">
<column name="dev_id" sql-type="int" not-null="true"/>
<generator class="native"/>
</id>
<property name="devName" type="string">
<column name="dev_name" sql-type="varchar(30)" not-null="true"/>
</property>
<property name="sn" type="string">
<column name="sn" sql-type="varchar(20)" not-null="true"/>
</property>
<property name="ip" type="string">
<column name="ip" sql-type="varchar(20)" not-null="false"/>
</property>
<property name="installDate" type="date">
<column name="install_date" sql-type="date" not-null="false"/>
</property>
<property name="memo" type="string">
<column name="memo" sql-type="varchar(255)" not-null="false"/>
</property>
<many-to-one name="devModel" not-null="true" column="model_id" class="com.omnet.test.DevModel" />
<many-to-one name="devLoc" not-null="true" column="loc_id" class="com.omnet.test.Location" />
<bag name="parent" lazy="true" table="device_relation">
<key column="child_id"/>
<many-to-many column="parent_id" class="com.omnet.test.Device"/>
</bag>
<joined-subclass
name="com.omnet.test.CompositeDevice"
table="composite_device"
dynamic-insert="true"
dynamic-update="true"
>
<key column="dev_id"/>
<many-to-one name="host" column="host_id" class="com.omnet.test.Device"/>
<property name="slot" not-null="true"/>
</joined-subclass>
</class>
the following is a test code:
public void addDevice()
{
Session sess = null;
try {
sess = sf.openSession();
CompositeDevice dev = new CompositeDevice();
dev.setDevName("blah");
dev.setSn("888999");
dev.setIp("172.20.101.12");
dev.setInstallDate( new Date() );
dev.setMemo( "test joined-subclass" );
dev.setDevModel( (DevModel)sess.load(DevModel.class,new Integer(4)) );
dev.setDevLoc( (Location)sess.load(Location.class,new Integer(4)) );
dev.setHost( (Device)sess.load(Device.class,new Integer(9)) );
dev.setSlot(1);
sess.save(dev);
sess.flush();
sess.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
the output is as below:
15:54:34,687 INFO [Environment] Hibernate 2.0.2
15:54:34,703 INFO [Environment] loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.query.imports=net.sf.hibernate.test, net.sf.hibernate.eg, hibernate.proxool.pool_alias=pool1, hibernate.connection.username=root, hibernate.connection.url=jdbc:mysql://172.20.101.12:3306/demo?useUnicode=true&characterEncoding=GBK, hibernate.show_sql=false, hibernate.connection.password=omnet123, hibernate.statement_cache.size=25, hibernate.connection.pool_size=1}
15:54:34,703 INFO [Environment] using java.io streams to persist binary types
15:54:34,703 INFO [Environment] using CGLIB reflection optimizer
15:54:34,703 INFO [Environment] JVM proxy support: true
15:54:34,703 INFO [Configuration] Mapping resource: com/omnet/test/Device.hbm.xml
15:54:35,359 INFO [Binder] Mapping class: com.omnet.test.DevClass -> dev_class
15:54:35,437 INFO [Binder] Mapping class: com.omnet.test.Icon -> icon
15:54:35,437 INFO [Binder] Mapping class: com.omnet.test.Location -> location
15:54:35,437 INFO [Binder] Mapping collection: com.omnet.test.Location.children -> location_relation
15:54:35,453 INFO [Binder] Mapping collection: com.omnet.test.Location.parent -> location_relation
15:54:35,453 INFO [Binder] Mapping class: com.omnet.test.DevModel -> dev_model
15:54:35,453 INFO [Binder] Mapping class: com.omnet.test.Device -> device
15:54:35,468 INFO [Binder] Mapping collection: com.omnet.test.Device.parent -> device_relation
15:54:35,484 INFO [Binder] Mapping joined-subclass: com.omnet.test.CompositeDevice -> composite_device
15:54:35,500 INFO [Configuration] processing one-to-many association mappings
15:54:35,500 INFO [Configuration] processing foreign key constraints
15:54:35,640 INFO [SessionFactoryImpl] building session factory
15:54:35,656 INFO [Dialect] Using dialect: net.sf.hibernate.dialect.MySQLDialect
15:54:35,656 INFO [DriverManagerConnectionProvider] Hibernate connection pool size: 1
15:54:35,656 INFO [DriverManagerConnectionProvider] using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://172.20.101.12:3306/demo?useUnicode=true&characterEncoding=GBK
15:54:35,671 INFO [DriverManagerConnectionProvider] connection properties: {user=root, password=omnet123}
15:54:35,671 INFO [PreparedStatementCache] prepared statement cache size: 25
15:54:35,671 INFO [SessionFactoryImpl] Use outer join fetching: true
15:54:35,828 INFO [SessionFactoryImpl] Use scrollable result sets: true
15:54:36,250 INFO [SessionFactoryObjectFactory] no JDNI name configured
15:54:36,250 INFO [SessionFactoryImpl] Query language substitutions: {no='N', true=1, yes='Y', false=0}
no error but no record in database :(
any suggestion is appreciated !
|