Hello all!
I am trying to do something extremely easy (as I read from the manuals) but it seems that I cannot achieve it. I am trying to create a many-to-many mapping between to tables.
A) I have a table called users:
Code:
mysql> select * from users;
+----+----------+----------+
| id | username | password |
+----+----------+----------+
| 1 | admin | admin |
| 2 | axel | axel |
| 3 | manos | manos |
+----+----------+----------+
3 rows in set (0.00 sec)
I have another table called artists:
Code:
mysql> select * from artist;
+----+--------------------------+
| id | artist |
+----+--------------------------+
| 1 | Jim Morrison |
| 2 | John Lennon |
| 3 | ACDC |
| 4 | Led Zeppelin |
+----+--------------------------+
4 rows in set (0.01 sec)
These two tables are connected by a third table called user_artist: (f_user_id is a foreign key to the table user and column id and f_artist_id is a foreign key to the table artist and column id)
Code:
mysql> select * from user_artist;
+-----------+-------------+----+
| f_user_id | f_artist_id | id |
+-----------+-------------+----+
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 1 | 4 | 4 |
| 2 | 1 | 5 |
| 2 | 3 | 6 |
| 3 | 2 | 7 |
+-----------+-------------+----+
7 rows in set (0.00 sec)
B) The corresponding hibernate mappings are as follows:
a) for the users table:
Code:
<hibernate-mapping>
<class name="com.am.struts.wamp3.app.business.Artist" table="artist">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="artist" column="artist" type="java.lang.String" />
<set name="users" table="user_artist" lazy="false" cascade="save-update">
<key column="f_artist_id"/>
<many-to-many class="com.am.struts.wamp3.user.business.SystemUsers" column="id"/>
</set>
</class>
</hibernate-mapping>
b) for the artist table:
Code:
<hibernate-mapping>
<class name="com.am.struts.wamp3.user.business.SystemUsers" table="users">
<id name="id" column="id" type="java.lang.Integer">
<generator class="native"/>
</id>
<property name="username" column="username" type="java.lang.String" />
<property name="password" column="password" type="java.lang.String" />
<set name="artists" table="user_artist" lazy="false" cascade="save-update">
<key column="f_user_id"/>
<many-to-many class="com.am.struts.wamp3.app.business.Artist" column="id"/>
</set>
</class>
</hibernate-mapping>
c) for the user_artist table there is no hibernate mapping, since it is referenced within the previous two mappings.
C) Finally, regarding the two java classes appart from the setters and getters of the String fields I also have the following methods:
i) Artist.java
Code:
public Set getusers()
{ return this.users; }
public void setusers(Set users)
{ this.users = users; }
ii) SystemUsers.java
Code:
public Set getartists()
{ return this.artists; }
public void setartists(Set artists)
{ this.artists = artists; }
D) The code in java that should produce a list of artists belonging to a specific user is as follows:
Code:
Session session = null;
Transaction tx = null;
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
List l = session.createQuery("from SystemUsers su where su.id = 3").list();
SystemUsers su = (SystemUsers)l.get(0);
Set arti = su.getartists();
tx.commit();
session.close();
return arti;
But this returns an empty Set.
Could you please help me out here.
Thank you in advance
axelmangr