Hi All,
Maps:
Code:
<hibernate-mapping>
<!-- com.diatem.db.qos.ViewContainer root -->
<class name="com.cache.test.ViewContainer" table="viewc">
<cache usage="read-write" />
<id name="id" type="long" column="id" unsaved-value="0">
<generator class="native"/>
</id>
<property name="name" column="name" type="string" not-null="true" unique="true"/>
<set name="neTemplates" inverse="true" cascade="all-delete-orphan" lazy="false">
<cache usage="read-write" />
<key column="view_ne_template"/>
<one-to-many class="com.cache.test.NetworkElementTemplate"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping default-cascade="none" auto-import="true" default-access="field">
<class name="com.cache.test.NetworkElementTemplate" table="ne_tmpl" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false">
<cache usage="read-write"/>
<id name="id" type="long" unsaved-value="0">
<generator class="native">
</generator>
</id>
<property name="creationUser" column="creationUser" type="string"/>
<property name="modificationUser" column="modificationUser" type="string"/>
<property name="lastModified" column="lastModified" type="timestamp"/>
<property name="creationTime" column="creationTime" type="timestamp"/>
<property name="name" type="string"/>
<property name="description" type="string"/>
<many-to-one name="view" column="view_ne_template" class="com.cache.test.ViewContainer"/>
</class>
</hibernate-mapping>
and code:
Code:
private void initHibernate() throws HibernateException {
// Load Configuration and build SessionFactory
Configuration cfg = new Configuration();
sessionFactory = cfg.configure().buildSessionFactory();
new SchemaExport(cfg).create(true, true);
//new SchemaUpdate(cfg).execute(true, true);
}
private void go() throws HibernateException{
initHibernate();
populateDb();
beginTransaction();
ViewContainer[] views = getViews();
endTransaction(false);
beginTransaction();
NetworkElementTemplate t1 =
(NetworkElementTemplate) session.load(
NetworkElementTemplate.class,
new Long(5));
endTransaction(false);
beginTransaction();
session.delete(t1);
endTransaction(true);
System.out.println("<<<<<<<<Deleted succesfully>>>>>>>>>");
beginTransaction();
views = getViews();
endTransaction(false);
System.out.println(views);
}
private void beginTransaction() throws HibernateException {
session = sessionFactory.openSession();
transaction = session.beginTransaction();
}
private void endTransaction(boolean commit) throws HibernateException {
if (commit) {
transaction.commit();
} else {
// Don't commit the transaction, can be faster for read-only operations
transaction.rollback();
}
session.close();
}
public ViewContainer[] getViews() {
Query q = null;
List newList = null;
try {
q =
session.createQuery(
"from viewc in class com.cache.test.ViewContainer");
newList = q.list();
} catch (HibernateException e) {
System.out.println("<<<<<<<getViews failed !!!>>>>>>>>");
e.printStackTrace();
System.exit(1);
}
ViewContainer[] views = new ViewContainer[newList.size()];
for (int i = 0; i < newList.size(); ++i) {
views[i] = (ViewContainer) newList.get(i);
}
System.out.println("<<<<<<<<<getViews OK.>>>>>>>>");
return views;
}
private void populateDb() throws HibernateException{
// add some stuff in the db ( 1 view + set of templates )
ViewContainer view = new ViewContainer();
view.setName("view1");
view.setNeTemplates(new HashSet());
for (int i = 0; i < 20; ++i) {
NetworkElementTemplate t = new NetworkElementTemplate();
t.setName("t_" + i);
view.getNeTemplates().add(t);
t.setView(view);
}
beginTransaction();
session.save(view);
endTransaction(true);
}
output:
Code:
10:32:02,408 INFO Environment:462 - Hibernate 2.1.3
10:32:02,424 INFO Environment:496 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=com.mysql.jdbc.Driver, hibernate.jdbc.fetch_size=0, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.ehcache.hibernate.Provider, hibernate.cache.use_query_cache=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.use_outer_join=false, hibernate.proxool.pool_alias=pool1, hibernate.connection.username=admin, hibernate.jdbc.use_scrollable_resultset=true, hibernate.connection.url=jdbc:mysql:///test1, hibernate.show_sql=true, hibernate.connection.password=}
10:32:02,424 INFO Environment:518 - using java.io streams to persist binary types
10:32:02,424 INFO Environment:519 - using CGLIB reflection optimizer
10:32:02,440 INFO Configuration:872 - configuring from resource: /hibernate.cfg.xml
10:32:02,440 INFO Configuration:844 - Configuration resource: /hibernate.cfg.xml
10:32:02,893 INFO Configuration:328 - Mapping resource: com/cache/test/NetworkElementTemplate.hbm.xml
10:32:03,049 INFO Binder:229 - Mapping class: com.cache.test.NetworkElementTemplate -> ne_tmpl
10:32:03,158 INFO Configuration:328 - Mapping resource: com/cache/test/QoSAll.hbm.xml
10:32:03,190 INFO Binder:229 - Mapping class: com.cache.test.ViewContainer -> viewc
10:32:03,205 INFO Configuration:1030 - Configured SessionFactory: null
10:32:03,205 INFO Configuration:613 - processing one-to-many association mappings
10:32:03,205 INFO Binder:1168 - Mapping collection: com.cache.test.ViewContainer.neTemplates -> ne_tmpl
10:32:03,205 INFO Configuration:622 - processing one-to-one association property references
10:32:03,221 INFO Configuration:647 - processing foreign key constraints
10:32:03,237 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.MySQLDialect
10:32:03,252 INFO SettingsFactory:55 - JDBC result set fetch size: 0
10:32:03,252 INFO SettingsFactory:62 - Use outer join fetching: false
10:32:03,252 INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
10:32:03,252 INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 20
10:32:03,268 INFO DriverManagerConnectionProvider:77 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql:///test1
10:32:03,268 INFO DriverManagerConnectionProvider:78 - connection properties: {user=admin, password=}
10:32:03,283 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
10:32:03,533 INFO SettingsFactory:102 - Use scrollable result sets: true
10:32:03,533 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): true
10:32:03,533 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
10:32:03,533 INFO SettingsFactory:114 - echoing all SQL to stdout
10:32:03,533 INFO SettingsFactory:117 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
10:32:03,533 INFO SettingsFactory:128 - cache provider: net.sf.ehcache.hibernate.Provider
10:32:03,549 INFO Configuration:1093 - instantiating and configuring caches
10:32:03,596 WARN Plugin:95 - Could not find configuration for com.cache.test.ViewContainer.neTemplates. Configuring using the defaultCache settings.
10:32:03,612 WARN Plugin:95 - Could not find configuration for com.cache.test.NetworkElementTemplate. Configuring using the defaultCache settings.
10:32:03,612 WARN Plugin:95 - Could not find configuration for com.cache.test.ViewContainer. Configuring using the defaultCache settings.
10:32:03,737 INFO SessionFactoryImpl:119 - building session factory
10:32:04,112 INFO SessionFactoryObjectFactory:82 - no JNDI name configured
10:32:04,112 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.MySQLDialect
10:32:04,112 INFO Configuration:613 - processing one-to-many association mappings
10:32:04,112 INFO Configuration:622 - processing one-to-one association property references
10:32:04,112 INFO Configuration:647 - processing foreign key constraints
10:32:04,112 INFO Configuration:613 - processing one-to-many association mappings
10:32:04,112 INFO Configuration:622 - processing one-to-one association property references
10:32:04,112 INFO Configuration:647 - processing foreign key constraints
10:32:04,127 INFO SchemaExport:98 - Running hbm2ddl schema export
10:32:04,127 INFO SchemaExport:117 - exporting generated schema to database
10:32:04,127 INFO DriverManagerConnectionProvider:42 - Using Hibernate built-in connection pool (not for production use!)
10:32:04,127 INFO DriverManagerConnectionProvider:43 - Hibernate connection pool size: 20
10:32:04,127 INFO DriverManagerConnectionProvider:77 - using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql:///test1
10:32:04,127 INFO DriverManagerConnectionProvider:78 - connection properties: {user=admin, password=}
drop table if exists ne_tmpl
drop table if exists viewc
create table ne_tmpl (
id BIGINT NOT NULL AUTO_INCREMENT,
creationUser VARCHAR(255),
modificationUser VARCHAR(255),
lastModified DATETIME,
creationTime DATETIME,
name VARCHAR(255),
description VARCHAR(255),
view_ne_template BIGINT,
primary key (id)
)
create table viewc (
id BIGINT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) not null unique,
primary key (id)
)
alter table ne_tmpl add index (view_ne_template), add constraint FK6CB807BDBC6E3768 foreign key (view_ne_template) references viewc (id)
10:32:04,315 INFO SchemaExport:160 - schema export complete
10:32:04,315 INFO DriverManagerConnectionProvider:143 - cleaning up connection pool: jdbc:mysql:///test1
Hibernate: insert into viewc (name) values (?)
Hibernate: insert into ne_tmpl (creationUser, modificationUser, lastModified, creationTime, name, description, view_ne_template) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ne_tmpl (creationUser, modificationUser, lastModified, creationTime, name, description, view_ne_template) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ne_tmpl (creationUser, modificationUser, lastModified, creationTime, name, description, view_ne_template) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ne_tmpl (creationUser, modificationUser, lastModified, creationTime, name, description, view_ne_template) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into ne_tmpl (creationUser, modificationUser, lastModified, creationTime, name, description, view_ne_template) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: select viewc.id as id, viewc.name as name from viewc viewc
Hibernate: select netemplate0_.id as id__, netemplate0_.view_ne_template as view_ne_8___, netemplate0_.id as id0_, netemplate0_.creationUser as creation2_0_, netemplate0_.modificationUser as modifica3_0_, netemplate0_.lastModified as lastModi4_0_, netemplate0_.creationTime as creation5_0_, netemplate0_.name as name0_, netemplate0_.description as descript7_0_, netemplate0_.view_ne_template as view_ne_8_0_ from ne_tmpl netemplate0_ where netemplate0_.view_ne_template=?
<<<<<<<<<getViews OK.>>>>>>>>
Hibernate: delete from ne_tmpl where id=?
<<<<<<<<Deleted succesfully>>>>>>>>>
Hibernate: select viewc.id as id, viewc.name as name from viewc viewc
Hibernate: select networkele0_.id as id0_, networkele0_.creationUser as creation2_0_, networkele0_.modificationUser as modifica3_0_, networkele0_.lastModified as lastModi4_0_, networkele0_.creationTime as creation5_0_, networkele0_.name as name0_, networkele0_.description as descript7_0_, networkele0_.view_ne_template as view_ne_8_0_ from ne_tmpl networkele0_ where networkele0_.id=?
<<<<<<<getViews failed !!!>>>>>>>>
net.sf.hibernate.UnresolvableObjectException: No row with the given identifier exists: 2, of class: com.cache.test.NetworkElementTemplate
at net.sf.hibernate.UnresolvableObjectException.throwIfNull(UnresolvableObjectException.java:38)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1943)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:68)
at net.sf.hibernate.type.EntityType.assemble(EntityType.java:130)
at net.sf.hibernate.collection.Set.initializeFromCache(Set.java:92)
at net.sf.hibernate.impl.SessionImpl.initializeCollectionFromCache(SessionImpl.java:3954)
at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3253)
at net.sf.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:336)
at net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3114)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:955)
at net.sf.hibernate.loader.Loader.list(Loader.java:946)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:834)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1536)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:39)
at com.cache.test.CacheTest.getViews(CacheTest.java:94)
at com.cache.test.CacheTest.go(CacheTest.java:62)
at com.cache.test.CacheTest.main(CacheTest.java:27)
If I remove the
Code:
<cache usage="read-write" />
from
Code:
<set name="neTemplates" inverse="true" cascade="all-delete-orphan" lazy="false">
<cache usage="read-write" />
<key column="view_ne_template"/>
<one-to-many class="com.cache.test.NetworkElementTemplate"/>
</set>
everything works !
TIA.