-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: 2nd level cache BUG/Problem when changing Hibernate Session
PostPosted: Fri Jun 25, 2004 7:01 am 
Beginner
Beginner

Joined: Wed May 12, 2004 4:27 am
Posts: 21
Location: Montreux (CH)
Hello,

I love cache... and I found either a bug or I use it wrong !!

Imagine this :

Person (id, name, getChildren(), getParents() );
PersonRelation (person_child_id, person_parent_id, int value);
this gives me PersonRelationPK .....

here is my problem.... I define queries in the xml file :
From Person
From PersonRelation
From Person p left join fetch p.children
From Person p left join fetch p.parents

then I do :
SESS1 = createSession()
for all queries Q, i++:
SESS1.getNamedQuery(Q).setCachable(true).setCacheRegion("HERE-" + i).list().iterator();

then I can do :

Person P = getPerson(ID=3)
for (Iterator iter = P.getChildren().iterator ; .....)
....

without problems.... (I means no SQL request done)

but if I close the Hibernate connection and create a new one :

Person P = getPerson(ID=3)
for (Iterator iter = P.getChildren().iterator ; .....)
....

for each relation, Hibernate does the following request :
select parent, child, type from personrelation where parent=? and child=?

what do I do wrong ??

please help me.... my cache would finally be ok :-)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 25, 2004 5:17 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
It seems like you want to cache the children collection of parent, rather than the named queries.

If you posted your mapping and code it would be easier for people to answer your question.


Top
 Profile  
 
 Post subject: 2nd level cache bugs when changing session ;o(
PostPosted: Sat Jun 26, 2004 6:29 am 
Beginner
Beginner

Joined: Wed May 12, 2004 4:27 am
Posts: 21
Location: Montreux (CH)
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();
    }
}




Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.