-->
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.  [ 2 posts ] 
Author Message
 Post subject: [SOLVED] Simple link table not being populated
PostPosted: Thu Oct 29, 2009 6:09 am 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
I seemed to have this working for a couple of minutes but then I re-factored the code and can't get it working again. I don't have any idea why it stopped working but I am completely stuck on it at the moment. I will mention that I am using Spring to manage Hibernate but I don't think that will be making any difference.

I am using HibernateTool to generate both the hbm.xml and the Java POJOs and have a simple database join as follows:

Code:
CREATE TABLE habitTrackerDomain (
  domain_id INT(10)  NOT NULL AUTO_INCREMENT,
  domain VARCHAR(255) NOT NULL UNIQUE,
  PRIMARY KEY (domain_id)
)   ENGINE = InnoDB
    AUTO_INCREMENT = 500;

CREATE TABLE habitTrackerUser (
  user_id INT(10)  NOT NULL AUTO_INCREMENT,
  user_id_in_domain VARCHAR(255)  NOT NULL,
  session_id CHAR(36),
  PRIMARY KEY (user_id),
  INDEX index_user_id_in_domain(user_id_in_domain)
)   ENGINE = InnoDB
    AUTO_INCREMENT = 500;

CREATE TABLE habitTrackerDomainToUser (
  domain_id INT(10)  NOT NULL,
  user_id INT(10)  NOT NULL,
  PRIMARY KEY (domain_id , user_id),
  CONSTRAINT fk_habitTrackerDomainToUser_domain_id FOREIGN KEY fk_habitTrackerDomainToUser_domain_id(domain_id)
    REFERENCES habitTrackerDomain(domain_id)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT,
  CONSTRAINT fk_habitTrackerDomainToUser_user_id FOREIGN KEY fk_habitTrackerDomainToUser_user_id(user_id)
    REFERENCES habitTrackerUser(user_id)
    ON DELETE RESTRICT
    ON UPDATE RESTRICT
)
ENGINE = InnoDB;


The generated hbm.xml files are:

Code:
<hibernate-mapping>
    <class name="uk.co.prodia.prosoc.domainmodel.HabitTrackerDomain" table="habitTrackerDomain" catalog="habittracker">
        <id name="domainId" type="java.lang.Integer">
            <column name="domain_id" />

            <generator class="identity" />
        </id>

        <property name="domain" type="string">
            <column name="domain" not-null="true" unique="true" />
        </property>

        <set name="habitTrackerUsers" inverse="false" table="habitTrackerDomainToUser">
            <key>
                <column name="domain_id" not-null="true" />
            </key>

            <many-to-many entity-name="uk.co.prodia.prosoc.domainmodel.HabitTrackerUser">
                <column name="user_id" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>


and:

Code:
<hibernate-mapping>
    <class name="uk.co.prodia.prosoc.domainmodel.HabitTrackerUser" table="habitTrackerUser" catalog="habittracker">
        <id name="userId" type="java.lang.Integer">
            <column name="user_id" />

            <generator class="identity" />
        </id>

        <property name="userIdInDomain" type="string">
            <column name="user_id_in_domain" not-null="true" />
        </property>

        <property name="sessionId" type="string">
            <column name="session_id" length="36" />
        </property>

        <set name="habitTrackerTrackers" inverse="true">
            <key>
                <column name="user_id" not-null="true" />
            </key>

            <one-to-many class="uk.co.prodia.prosoc.domainmodel.HabitTrackerTracker" />
        </set>

        <set name="habitTrackerDomains" inverse="true" table="habitTrackerDomainToUser">
            <key>
                <column name="user_id" not-null="true" />
            </key>

            <many-to-many entity-name="uk.co.prodia.prosoc.domainmodel.HabitTrackerDomain">
                <column name="domain_id" not-null="true" />
            </many-to-many>
        </set>
    </class>
</hibernate-mapping>


and my Java code for saving the HabitTrackerUser object is:

Quote:
HabitTrackerUser habitTrackerUser = new HabitTrackerUser(username);
HabitTrackerDomain habitTrackerDomain = new HabitTrackerDomain(domainName);

habitTrackerDomain.getHabitTrackerUsers().add(habitTrackerUser);
habitTrackerUser.getHabitTrackerDomains().add(habitTrackerDomain); // Must load up both sides in the m-n relentionship for hibernate

save(habitTrackerUser);


It all looks OK to me:

1) The inverse parameter in the hbm.xml file looks like it is being correctly generated;
2) I have loaded up both sides of the relationship in the Java code so that each object possesses the other object;
3) I have tried to call save on the other object but it complains about the duplicate entry for domain_name, as it should;
4) I have tried to use getCurrentSession().flush() but it doesn't seem to make any difference;
5) I have checked that the objects are correctly populated on both sides just before calling save and they are both OK.

The only table that get populated is the habitTrackerUser table, the habitTrackerDomainToUser table is ignored as is the habitTrackerDomain table if the UNIQUE field restriction on domain_name is removed.

Does anyone have any suggestions that maybe helpful? Thanks in advance.


Top
 Profile  
 
 Post subject: Re: [SOLVED] Simple link table not being populated
PostPosted: Thu Oct 29, 2009 6:41 am 
Beginner
Beginner

Joined: Thu Mar 27, 2008 8:49 am
Posts: 27
A simple change was all that was required to fix this. Where I was creating a new HabitTrackerDomain I should have passed in the already existing one. So, I had method signatures such as:

Code:
public HabitTrackerUser createHabitTrackerUser(String domainName , String username)


and I should have had:

Code:
public HabitTrackerUser createHabitTrackerUser(HabitTrackerDomain habitTrackerDomain, String username)


An existing HabitTrackerDomain should have been retrieved where I was creating a new one. I now retrieve it as follows:

Code:
habitTrackerDomain = ifaceDAOHabitTrackerDomain.getHabitTrackerDomain(clientDomainName);


where the ifaceDAOHabitTrackerDomain.getHabitTrackerDomain(clientDomainName) is a call to a method that has the HSQL select statement in it:

Quote:
String query = "from HabitTrackerDomain where domainId = :habitTrackerDomainId";
return (HabitTrackerDomain) getCurrentSession().createQuery(query).setInteger("habitTrackerDomainId" , habitTrackerDomainId).uniqueResult();


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.