Hey everyone,
I'm new to hibernate, so forgive me if this question is 'elementary', but I have been searching for a while and have found nothing, so I hope that someone here can help.
What I have is 2 classes which I am trying to populate from a single database. I have 1 parent 'news' class with 2 child classes 'dailyNews' and 'generalNews'. There is also an 'affiliateNews' class. Each affiliateNews class has exactely 1 generalNews object and can have multiple dailyNews objects. Each news object relates to only 1 affiliate.
The database structure we have been working with contains all of the data in 1 table. There is a 'newsType' field which indicates whether the news is general or daily. There is also a field called 'affiliateID' which identifies the affiliateNews object.
We were trying to have hibernate populate the affiliateNews object which contains 1 member variable of type generalNews called 'generalNews' and 1 member variable which is a sorted set called 'dailyNews'. We want to retrieve all of the dailyNews objects as a sorted set, and set them in 'dailyNews' and retrieve the sole generalNews object and set it in 'generalNews'.
Using the below mapping files, we can get this to compile and run, but the sortedset in the AffiliateMapping object retrieves both the 'GeneralNews' and the 'DailyNews' objects. We have been unsuccessful in mapping the 'GeneralNews' object to its member variable.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<!-- Auto-generated mapping file from the hibernate.org cfg2hbm engine -->
<class name="com.adsmack.smacklet.news.domain.AffiliateNews" table="news">
<id name="affiliateID" type="integer">
<column name="id" />
<generator class="increment" />
</id>
<set name="dailyNews" table="news" sort="natural">
<key column="affiliateID" not-null="true" />
<one-to-many class="com.adsmack.smacklet.news.domain.AffiliateDailyNews" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping default-lazy="false">
<!-- Auto-generated mapping file from the hibernate.org cfg2hbm engine -->
<class name="com.adsmack.smacklet.news.domain.News" table="news" >
<id name="id" type="integer">
<column name="ID" />
<generator class="increment" />
</id>
<discriminator column="newstype" />
<property name="article" type="string">
<column name="article" />
</property>
<subclass name="com.adsmack.smacklet.news.domain.AffiliateDailyNews"
discriminator-value="DAILYNEWS" >
<property name="dateSubmitted" type="timestamp">
<column name="dateSubmitted" />
</property>
<property name="title" type="string">
<column name="title" />
</property>
<property name="articleLink" type="string">
<column name="articlelink" />
</property>
<property name="linkText" type="string">
<column name="linktext" />
</property>
</subclass>
<subclass name="com.adsmack.smacklet.news.domain.AffiliateGeneralNews"
discriminator-value="GENERALNEWS" />
</class>
</hibernate-mapping>
DB Schema ....
DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL auto_increment,
`article` text NOT NULL,
`datesubmitted` datetime NOT NULL default '0000-00-00 00:00:00',
`title` varchar(100) NOT NULL default '',
`articlelink` varchar(100) default NULL,
`linktext` varchar(50) default NULL,
`newstype` varchar(45) NOT NULL default '',
`affiliateid` int(10) unsigned NOT NULL default '0',
PRIMARY KEY (`id`),
KEY `FK_news_1` (`affiliateid`),
CONSTRAINT `FK_news_1` FOREIGN KEY (`affiliateid`) REFERENCES `affiliatenews` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Thanks you
|