OK This is my code that is very simple
CoreUsersgroup have id, name + children and parents.
Children & Parents are CoreUserMembership (user_parent_id, user_child_id, type_of_relation)... CoreUserMembershipPK is then the key (parent_id, child_id)
My problem is the following :
it works perfectly when it is in the same session....
but when I close the session and open a new one...
it always does the following sql request :
select parent_id, child_id, type from membership where paren_id = ? and child_id = ?
and this for each relations... It means for me, that it knows from cache the CoreUsergroup object... and it knows too that this object has 4 children and 2 parents for example.. because it does the number of request corresponding to 6... (or 7 if I add a child...)
it is very strange....
Code:
---------------------------
HERE IS MY TEST
for (int i = 0 ; i < QUERIES.length ; i++) {
hib.getNamedQuery(QUERIES[i]).setCacheable(true).setCacheRegion(CACHE_REGION).list().iterator().next();
}
// all requests are done !!!
if ( 1== 1 ) {
System.out.println("A---1");
CoreUsergroup user = getUserByLogin("samfra"); // loop in the query (From CoreUsergroup) to find the right one...
System.out.println("A---2");
user.getParents().iterator();
System.out.println("A---3");
user.getChildren().iterator();
System.out.println("A---4");
}
// until there.. no request are done
// close HIB, open a new one (Hib' session)
if ( 1== 1 ) {
System.out.println("A---1");
CoreUsergroup user = getUserByLogin("samfra"); // loop in the query (From CoreUsergroup) to find the right one...
System.out.println("A---2");
user.getParents().iterator();
// it does #parents of requests ;o(
System.out.println("A---3");
user.getChildren().iterator();
// it does #children of requests ;o(
System.out.println("A---4");
}
---------------------------
/**
* @hibernate.class table="core_usersgroups"
* @hibernate.cache usage = "read-write"
* @hibernate.query name = "load-usersgroup" query = "From CoreUsersgroup"
* @hibernate.query name = "load-usermembership" query = "From CoreUserMembership"
* @hibernate.query name = "load-usersgroup-passwords"
* query = "From CoreUsersgroup user left join fetch user.passwords"
* @hibernate.query name = "load-usersgroup-children"
* query = "From CoreUsersgroup user left join fetch user.children"
* @hibernate.query name = "load-usersgroup-parents"
* query = "From CoreUsersgroup user left join fetch user.parents"
* @hibernate.query name = "load-usersgroup-rights"
* query = "From CoreUsersgroup user left join fetch user.rights"
*
*/
public class CoreUsersgroup implements Serializable, Xmlizable {
private Integer id;
private String name;
private Set lesEnfants;
private Set lesParents;
/** full constructor */
public CoreUsersgroup(Integer id, String name, Set lesEnfants, Set lesParents) {
this.id = id;
this.name = name;
this.lesEnfants = lesEnfants;
this.lesParents = lesParents;
}
public CoreUsersgroup() {
}
/**
* @hibernate.id generator-class="hilo" type="java.lang.Integer" column="id" unsaved-value = "-1"
* @hibernate.generator-param name="table" value="hibernate_unique_key"
* @hibernate.generator-param name="column" value="core_usersgroups"
* @hibernate.generator-param name="max_lo" value="0"
*
*/
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
/**
* @hibernate.property column="name" length="50" not-null="true"
*
*/
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
/**
* @hibernate.set lazy="false" inverse="true" cascade = "none"
* @hibernate.collection-key column="usergroup_parent"
* @hibernate.collection-one-to-many class="utopix.centrix.mapping.core.usergroups.CoreUserMembership"
* @hibernate.collection-cache usage = "read-write"
*
*/
public Set getChildren() {
return this.lesEnfants;
}
public void setChildren(Set lesEnfants) {
this.lesEnfants = lesEnfants;
}
/**
* @hibernate.set lazy="false" inverse="true" cascade = "none"
* @hibernate.collection-key column="usergroup_child"
* @hibernate.collection-one-to-many class="utopix.centrix.mapping.core.usergroups.CoreUserMembership"
* @hibernate.collection-cache usage = "read-write"
*
*/
public Set getParents() {
return this.lesParents;
}
public void setParents(Set lesParents) {
this.lesParents = lesParents;
}
public String toString() {
return new ToStringBuilder(this).append("id", getId()).toString();
}
}
/**
* @hibernate.class table="core_user_membership"
* @hibernate.cache usage = "read-write"
*
*/
public class CoreUserMembership implements Serializable {
private CoreUserMembershipPK comp_id;
private int type;
public CoreUserMembership(CoreUserMembershipPK comp_id, int type) {
this.comp_id = comp_id;
this.type = type;
}
public CoreUserMembership() { }
/**
* @hibernate.id unsaved-value = "any"
* @return
*/
public CoreUserMembershipPK getRelation() {
return this.comp_id;
}
public void setRelation(CoreUserMembershipPK comp_id) {
this.comp_id = comp_id;
}
/**
* @hibernate.property
* column="type"
* length="10"
* not-null="true"
*
*/
public int getType() {
return this.type;
}
public void setType(int type) {
this.type = type;
}
public String toString() {
return new ToStringBuilder(this).append("comp_id", getRelation())
.toString();
}
public boolean equals(Object other) {
if ((this == other)) {
return true;
}
if (!(other instanceof CoreUserMembership)) {
return false;
}
CoreUserMembership castOther = (CoreUserMembership) other;
return new EqualsBuilder().append(this.getRelation(),
castOther.getRelation()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getRelation()).toHashCode();
}
}
/**
* @hibernate.id
* generator-class="assigned"
* @hibernate.cache usage = "read-write"
*
*
*/
public class CoreUserMembershipPK implements Serializable {
private CoreUsersgroup child;
private CoreUsersgroup parent;
public CoreUserMembershipPK(CoreUsersgroup child, CoreUsersgroup parent) {
this.child = child;
this.parent = parent;
}
public CoreUserMembershipPK() {
}
/**
* @hibernate.many-to-one column="usergroup_child" cascade = "all"
*
*/
public CoreUsersgroup getChild() {
return this.child;
}
public void setChild(CoreUsersgroup child) {
this.child = child;
}
/**
* @hibernate.many-to-one column="usergroup_parent"
*
*/
public CoreUsersgroup getParent() {
return this.parent;
}
public void setParent(CoreUsersgroup parent) {
this.parent = parent;
}
public String toString() {
return new ToStringBuilder(this).append("Child", getChild())
.append("Parent", getParent()).toString();
}
public boolean equals(Object other) {
if ((this == other)) {
return true;
}
if (!(other instanceof CoreUserMembershipPK)) {
return false;
}
CoreUserMembershipPK castOther = (CoreUserMembershipPK) other;
return new EqualsBuilder().append(this.getChild(), castOther.getChild())
.append(this.getParent(),
castOther.getParent()).isEquals();
}
public int hashCode() {
return new HashCodeBuilder().append(getChild())
.append(getParent())
.toHashCode();
}
}