Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Please help me. thanks in advance
I developed a small app. has only four classes: DeviceData has DeviceID and a map of DeviceVarValue. DeviceVarValue has 2 types (String, measurement). I use enum type as descriminator. the db is hsql. the other stuff is based on caveatempor demo app.
-----------------------------------------------------
public class DeviceID {
private String id;
private String domain;
}
public class DeviceData {
private DeviceID deviceId;
private Date timestamp;
private long sequence;
private long devDataId = 0;//id
private Map<String, DeviceVarValue> properties = new HashMap<String, DeviceVarValue>();
}
public class DeviceVarValue {
public enum Type {
STRING, MEASUREMENT;
}
private long varId = 0;//id
private DeviceVarValue.Type type;
private String key;
private String value;
private int devDataId;
}
public class MeasurementValue extends DeviceVarValue {
private String unit;
private int precision;
}
mapping file is like:-------------------------------------------------
<hibernate-mapping package="com.gehc.device.db.model">
<class name="DeviceID"
table="device_id">
<id name="id"
type="string"
column="dev_id">
<generator class="assigned" />
</id>
<property name="domain"
type="string"
column="domain"
length="50" />
</class>
<class name="DeviceData"
table="device_data">
<id name="devDataId"
type="long"
column="dev_data_id"
unsaved-value="null"
access="field">
<generator class="increment" />
</id>
<many-to-one name="deviceId"
class="DeviceID"
column="dev_id"
cascade="none"
not-null="true"
access="field"
insert="false"
update="false"/>
<property name="sequence"
type="long"
column="sequence"
not-null="true"
update="false"/>
<property name="timestamp"
type="timestamp"
column="time_stamp"
not-null="true"
update="false"/>
<map name="properties"
table="device_var_value">
<!--key: foreign key in collection table-->
<key column="dev_data_id"/>
<!--map-key: key of the map-->
<map-key column="key" type="string"/>
<!--<element type="string" column="value" not-null="true"/>-->
<one-to-many class="com.gehc.device.db.model.DeviceVarValue"/>
</map>
</class>
<typedef class="com.gehc.device.db.persistence.StringEnumUserType" name="val_type">
<param name="enumClassname">com.gehc.device.db.model.DeviceVarValue$Type</param>
</typedef>
<class name="DeviceVarValue" table="device_var_value" lazy="true" discriminator-value="STRING">
<id name="varId" type="long"
column="var_Id"
unsaved-value="null"
access="field">
<generator class="increment" />
</id>
<discriminator
column="type"
type="val_type"/>
<property name="devDataId" type="int" column="dev_data_id" />
<property name="key" type="string" column="key" />
<property name="value" type="string" column="value" />
<!--<property name="type" type="val_type" column="type" />-->
<subclass name="MeasurementValue" discriminator-value="MEASUREMENT">
<join table="measurement_value" fetch="select">
<key column="var_id" foreign-key="fk_mea_val"/>
<property name="unit"
type="string"
column="unit"
length="50"
update="false"
not-null="true"
access="field"/>
<property name="precision"
type="integer"
column="precision"
not-null="true"
access="field"/>
</join>
</subclass>
</class>
</hibernate-mapping>
testing code is:-----------------------------------------------------
DeviceIDDAO devIDDAO = DAOFACTORY.getDeviceIDDAO();
DeviceVarValueDAO varDAO = DAOFACTORY.getDeviceVarValueDAO();
DeviceDataDAO dataDAO = DAOFACTORY.getDeviceDataDAO();
// deviceid
devID1 = new DeviceID("id1", "somewhere");
devIDDAO.makePersistent(devID1);
// varvalue
value1 = new DeviceVarValue(DeviceVarValue.Type.STRING, "key1", "value1");
value2 = new MeasurementValue(DeviceVarValue.Type.MEASUREMENT, "key3", "value3", "unit1", 90);
data1 = new DeviceData(devID1, Calendar.getInstance().getTime(), 9999);
data1.getProperties().put("key1", value1);
// data1.getProperties().put("key2", value2);
dataDAO.makePersistent(data1);
-------------------------------------------------------------
And the problems are:
1. I can't put DeviceID and DeviceData in two mapping file. I got error:
An association from the table device_data refers to an unmapped class: com.gehc.device.db.model.DeviceID
---------------------------------------------------
2. when i run the above code. i got error:
5:18:34,406 INFO Environment:499 - Hibernate 3.2 cr2
15:18:34,406 INFO Environment:517 - loaded properties from resource hibernate.properties: {hibernate.order_updates=true, hibernate.default_batch_fetch_size=8, hibernate.connection.driver_class=org.hsqldb.jdbcDriver, hibernate.cache.provider_class=org.hibernate.cache.HashtableCacheProvider, hibernate.cache.use_query_cache=true, hibernate.max_fetch_depth=1, hibernate.dialect=org.hibernate.dialect.HSQLDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=sa, hibernate.cache.region_prefix=hibernate.test, hibernate.connection.url=jdbc:hsqldb:., hibernate.bytecode.use_reflection_optimizer=false, hibernate.connection.password=****, hibernate.jdbc.batch_versioned_data=true, hibernate.connection.pool_size=1}
15:18:34,406 INFO Environment:548 - using java.io streams to persist binary types
15:18:34,406 INFO Environment:666 - Bytecode provider name : cglib
15:18:34,422 INFO Environment:583 - using JDK 1.4 java.sql.Timestamp handling
15:18:34,500 INFO Configuration:1345 - configuring from resource: /hibernate.cfg.xml
15:18:34,500 INFO Configuration:1322 - Configuration resource: /hibernate.cfg.xml
15:18:34,625 INFO Configuration:502 - Reading mappings from resource: com/gehc/device/db/model/DeviceVarValue.hbm.xml
15:18:34,750 INFO HbmBinder:298 - Mapping class: com.gehc.device.db.model.DeviceVarValue -> device_var_value
15:18:34,813 INFO HbmBinder:815 - Mapping subclass: com.gehc.device.db.model.MeasurementValue -> device_var_value
15:18:34,828 INFO HbmBinder:939 - Mapping class join: com.gehc.device.db.model.MeasurementValue -> measurement_value
15:18:34,828 INFO Configuration:502 - Reading mappings from resource: com/gehc/device/db/model/DeviceData.hbm.xml
15:18:34,875 INFO HbmBinder:298 - Mapping class: com.gehc.device.db.model.DeviceID -> device_id
15:18:34,875 INFO HbmBinder:298 - Mapping class: com.gehc.device.db.model.DeviceData -> device_data
15:18:34,953 INFO Configuration:1460 - Configured SessionFactory: null
15:18:34,953 INFO HbmBinder:2346 - Mapping collection: com.gehc.device.db.model.DeviceData.properties -> device_var_value
15:18:34,969 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
15:18:34,969 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 1
15:18:34,969 INFO DriverManagerConnectionProvider:45 - autocommit mode: false
15:18:34,969 INFO DriverManagerConnectionProvider:80 - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost
15:18:34,969 INFO DriverManagerConnectionProvider:86 - connection properties: {user=sa, password=****}
15:18:35,063 INFO SettingsFactory:78 - RDBMS: HSQL Database Engine, version: 1.8.0
15:18:35,063 INFO SettingsFactory:79 - JDBC driver: HSQL Database Engine Driver, version: 1.8.0
15:18:35,094 INFO Dialect:128 - Using dialect: org.hibernate.dialect.HSQLDialect
15:18:35,110 INFO TransactionFactoryFactory:31 - Using default transaction strategy (direct JDBC transactions)
15:18:35,110 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
15:18:35,110 INFO SettingsFactory:126 - Automatic flush during beforeCompletion(): disabled
15:18:35,110 INFO SettingsFactory:130 - Automatic session close at end of transaction: disabled
15:18:35,125 INFO SettingsFactory:145 - Scrollable result sets: enabled
15:18:35,125 INFO SettingsFactory:153 - JDBC3 getGeneratedKeys(): disabled
15:18:35,125 INFO SettingsFactory:161 - Connection release mode: auto
15:18:35,125 INFO SettingsFactory:185 - Maximum outer join fetch depth: 3
15:18:35,125 INFO SettingsFactory:188 - Default batch fetch size: 8
15:18:35,125 INFO SettingsFactory:192 - Generate SQL with comments: disabled
15:18:35,125 INFO SettingsFactory:196 - Order SQL updates by primary key: enabled
15:18:35,125 INFO SettingsFactory:357 - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
15:18:35,125 INFO ASTQueryTranslatorFactory:24 - Using ASTQueryTranslatorFactory
15:18:35,125 INFO SettingsFactory:204 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
15:18:35,125 INFO SettingsFactory:210 - Second-level cache: enabled
15:18:35,125 INFO SettingsFactory:214 - Query cache: disabled
15:18:35,125 INFO SettingsFactory:344 - Cache provider: org.hibernate.cache.NoCacheProvider
15:18:35,125 INFO SettingsFactory:229 - Optimize cache for minimal puts: disabled
15:18:35,141 INFO SettingsFactory:234 - Cache region prefix: hibernate.test
15:18:35,141 INFO SettingsFactory:238 - Structured second-level cache entries: disabled
15:18:35,141 INFO SettingsFactory:258 - Echoing all SQL to stdout
15:18:35,141 INFO SettingsFactory:265 - Statistics: disabled
15:18:35,141 INFO SettingsFactory:269 - Deleted entity synthetic identifier rollback: disabled
15:18:35,141 INFO SettingsFactory:284 - Default entity-mode: pojo
15:18:35,172 INFO SessionFactoryImpl:159 - building session factory
15:18:35,578 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
15:18:35,594 INFO SchemaExport:152 - Running hbm2ddl schema export
15:18:35,594 DEBUG SchemaExport:168 - import file not found: /import.sql
15:18:35,594 INFO SchemaExport:177 - exporting generated schema to database
15:18:35,594 DEBUG SchemaExport:301 -
alter table device_data
drop constraint FKA4020FF3D7BB4DB
15:18:35,594 DEBUG SchemaExport:301 -
alter table device_var_value
drop constraint FKCADF27B0203D22EB
15:18:35,610 DEBUG SchemaExport:286 - Unsuccessful: alter table device_var_value drop constraint FKCADF27B0203D22EB
15:18:35,610 DEBUG SchemaExport:287 - Constraint not found FKCADF27B0203D22EB in table: DEVICE_VAR_VALUE in statement [alter table device_var_value drop constraint FKCADF27B0203D22EB]
15:18:35,610 DEBUG SchemaExport:301 -
alter table measurement_value
drop constraint fk_mea_val
15:18:35,610 DEBUG SchemaExport:301 -
drop table device_data if exists
15:18:35,610 DEBUG SchemaExport:301 -
drop table device_id if exists
15:18:35,610 DEBUG SchemaExport:301 -
drop table device_var_value if exists
15:18:35,610 DEBUG SchemaExport:301 -
drop table measurement_value if exists
15:18:35,610 DEBUG SchemaExport:301 -
create table device_data (
dev_data_id bigint not null,
dev_id varchar(255) not null,
sequence bigint not null,
time_stamp timestamp not null,
primary key (dev_data_id)
)
15:18:35,610 DEBUG SchemaExport:301 -
create table device_id (
dev_id varchar(255) not null,
domain varchar(50),
primary key (dev_id)
)
15:18:35,610 DEBUG SchemaExport:301 -
create table device_var_value (
var_Id bigint not null,
type varchar(255) not null,
dev_data_id integer,
key varchar(255),
value varchar(255),
primary key (var_Id)
)
15:18:35,625 DEBUG SchemaExport:301 -
create table measurement_value (
var_id bigint not null,
unit varchar(50) not null,
precision integer not null,
primary key (var_id)
)
15:18:35,625 DEBUG SchemaExport:301 -
alter table device_data
add constraint FKA4020FF3D7BB4DB
foreign key (dev_id)
references device_id
15:18:35,625 DEBUG SchemaExport:301 -
alter table device_var_value
add constraint FKCADF27B0203D22EB
foreign key (dev_data_id)
references device_data
15:18:35,625 ERROR SchemaExport:272 - Unsuccessful: alter table device_var_value add constraint FKCADF27B0203D22EB foreign key (dev_data_id) references device_data
15:18:35,625 ERROR SchemaExport:273 - Column types do not match in statement [alter table device_var_value add constraint FKCADF27B0203D22EB foreign key (dev_data_id) references device_data]
15:18:35,625 DEBUG SchemaExport:301 -
alter table measurement_value
add constraint fk_mea_val
foreign key (var_id)
references device_var_value
15:18:35,625 INFO SchemaExport:194 - schema export complete
Hibernate:
select
deviceid_.dev_id,
deviceid_.domain as domain2_
from
device_id deviceid_
where
deviceid_.dev_id=?
Hibernate:
select
deviceid_.dev_id,
deviceid_.domain as domain2_
from
device_id deviceid_
where
deviceid_.dev_id=?
Hibernate:
select
deviceid_.dev_id,
deviceid_.domain as domain2_
from
device_id deviceid_
where
deviceid_.dev_id=?
Hibernate:
insert
into
device_id
(domain, dev_id)
values
(?, ?)
Hibernate:
insert
into
device_id
(domain, dev_id)
values
(?, ?)
Hibernate:
insert
into
device_id
(domain, dev_id)
values
(?, ?)
Hibernate:
update
device_var_value
set
dev_data_id=null,
key=null
where
dev_data_id=?
Hibernate:
update
device_var_value
set
dev_data_id=?,
key=?
where
var_Id=?
15:18:35,891 ERROR AbstractFlushingEventListener:300 - Could not synchronize database state with session
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1082)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:993)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:301)
at $Proxy0.flush(Unknown Source)
at com.gehc.device.db.test.TestCaseWithData.inTransaction(TestCaseWithData.java:163)
at com.gehc.device.db.test.HibernateTest.runTest(HibernateTest.java:31)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
-------------------------------------------------
3. i can't insert 2 rows into device_data table. because of duplicate id.
any suggestions? I really appreicate your help!!!!