Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: ManyToMany with JoinTable brings back null List or Set
PostPosted: Tue May 29, 2012 4:41 pm 
Newbie

Joined: Mon Nov 21, 2005 11:18 pm
Posts: 6
Java 1.6
Hibernate 3.5.0-FINAL
Mysql 5.5

I'm convinced I'm doing something wrong here but I just can't figure out what it is. I've tried using a List and Set member variable for this relationship and both give me the same result, which is a null object. The join table is loaded with proper data and when I turn up Hibernate logging and find the sql that's executed (see below), I can paste this sql into my DBVis application and I DO get back the result I'm expecting. Here's what I have configured:

Consumer.java (my entity with the @ManyToMany annotation)
Code:
@Entity
@Table(name = "CONSUMER")
@XmlRootElement(name = "consumer")
@XmlAccessorType(XmlAccessType.NONE)
public class Consumer implements Persistable<Long>, Serializable {

...

@ManyToMany
@JoinTable(name = "CONSUMER_TAG",
    joinColumns = @JoinColumn(name = "CONSUMER_UUID", referencedColumnName = "PRIM_UUID"),
    inverseJoinColumns = @JoinColumn(name = "TAG_ID", referencedColumnName = "TAG_ID")
)
@XmlElement
private List<Tag> tags;

}


Tag.java
Code:
@Entity
@Table(name = "TAG")
@XmlRootElement(name = "tag")
@XmlAccessorType(XmlAccessType.NONE)

public class Tag implements Persistable<Long>, Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "TAG_ID")
    private Long id;

    @Column(name = "NAME")
    private String name;


    public Tag() {
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


CONSUMER_TAG table
Code:
CREATE TABLE IF NOT EXISTS consumer_tag (
  consumer_uuid varchar(255) NOT NULL,
  tag_id int(11) NOT NULL,
  INDEX consumer_uuid_idx (consumer_uuid),
  INDEX tag_id_idx (tag_id),
  FOREIGN KEY (consumer_uuid) REFERENCES consumer(prim_uuid),
  FOREIGN KEY (tag_id) REFERENCES tag(tag_id)
);


The SQL that Hibernate generates for this ManyToMany annotation is:
Code:
select
        tags0_.CONSUMER_UUID as CONSUMER1_5_1_,
        tags0_.TAG_ID as TAG2_1_,
        tag1_.TAG_ID as TAG1_20_0_,
        tag1_.NAME as NAME20_0_
from
        CONSUMER_TAG tags0_
inner join
        TAG tag1_ on tags0_.TAG_ID=tag1_.TAG_ID
where
        tags0_.CONSUMER_UUID=?


If I substitute a real value for the CONSUMER_UUID constraint and run this in DBVis, I _do_ get back a single result. But when I call a consumer.getTags() on my Consumer bean, the result is null.

Does anyone see something I'm doing awfully wrong?

Thanks so much for any help or suggestions!

- Billy -


Top
 Profile  
 
 Post subject: Re: ManyToMany with JoinTable brings back null List or Set
PostPosted: Wed May 30, 2012 12:01 pm 
Newbie

Joined: Mon Nov 21, 2005 11:18 pm
Posts: 6
After some additional debugging, I found a ton of these hibernate DEBUG level statements in my logs, indicating that a collection was found for my Consumer.tags relationship but it says (uninitialized) next to it. What does this mean under the hood other than this is likely the reason why my getTags() method on my Consumer object is returning null for all Consumers.

Any one have an idea?

10346624 [http-8080-1] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [com.liveensure.api.admin.Consumer#877]
10346625 [http-8080-1] DEBUG org.hibernate.engine.StatefulPersistenceContext - initializing non-lazy collections
10346626 [http-8080-1] DEBUG org.hibernate.transaction.JDBCTransaction - commit
10346627 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - processing flush-time cascades
10347354 [http-8080-1] DEBUG org.hibernate.event.def.AbstractFlushingEventListener - dirty checking collections
10347362 [http-8080-1] DEBUG org.hibernate.engine.Collections - Collection found: [com.liveensure.api.admin.Consumer.tags#com.liveensure.api.admin.Consumer@13e993df], was: [com.liveensure.api.admin.Consumer.tags#com.liveensure.api.admin.Consumer@13e993df] (uninitialized)
10347363 [http-8080-1] DEBUG org.hibernate.engine.Collections - Collection found: [com.liveensure.api.admin.Consumer.tags#com.liveensure.api.admin.Consumer@33ccedc3], was: [com.liveensure.api.admin.Consumer.tags#com.liveensure.api.admin.Consumer@33ccedc3] (uninitialized)
10347364 [http-8080-1] DEBUG org.hibernate.engine.Collections - Collection found: [com.liveensure.api.admin.Consumer.tags#com.liveensure.api.admin.Consumer@530db7c6], was: [com.liveensure.api.admin.Consumer.tags#com.liveensure.api.admin.Consumer@530db7c6] (uninitialized)


Top
 Profile  
 
 Post subject: Re: ManyToMany with JoinTable brings back null List or Set
PostPosted: Wed May 30, 2012 5:16 pm 
Newbie

Joined: Mon Nov 21, 2005 11:18 pm
Posts: 6
Mystery solved...

In turns out that a developer on our team overrode the clone() method on the Consumer object I reference below and I did not add my new 'tags' member variable to this method which loads a new Consumer object. After I added c.tags = this.tags to this clone() method, my Set was loaded appropriately.

Not likely this will help anyone since I'm not sure why the clone method was overridden here but maybe someone will stumble upon this thread with a similar case.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.