Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0 beta 4
Mapping documents:
Code:
<hibernate-mapping package="p">
<class name="User" table="users">
<id name="id" column="id" type="integer">
<generator class="native" />
</id>
<property name="username" column="username" type="string" length="20" not-null="true" />
<property name="password" column="password" type="string" length="20" not-null="true" />
<bag name="books" table="collection" lazy="true">
<key column="user_id" />
<many-to-many column="book_id" class="Book" />
<element column="price" type="string" />
</bag>
</class>
</hibernate-mapping>
Code:
<hibernate-mapping package="p">
<class name="Book" table="books">
<id name="id" column="id" type="integer" >
<generator class="native"/>
</id>
<property name="ISBN" column="ISBN" type="string" length="10" not-null="true" />
<property name="title" column="title" type="string" length="250" />
<property name="year" column="year" type="string" length="4" />
<bag name="owners" table="collection">
<key column="book_id" />
<many-to-many column="user_id" class="User" />
<element column="price" type="string" />
</bag>
</class>
</hibernate-mapping>
The generated SQL (show_sql=true):Code:
alter table collectie drop foreign key FK7043CBC637DFAB81;
alter table collectie drop foreign key FK7043CBC61999E1E;
drop table if exists users;
drop table if exists collection;
drop table if exists books;
create table users (
id integer not null auto_increment,
username varchar(20) not null,
password varchar(20) not null,
primary key (id)
);
create table collection (
book_id integer not null,
user_id integer not null
);
create table books (
id integer not null auto_increment,
ISBN varchar(10) not null,
title varchar(250),
year varchar(4),
primary key (id)
);
alter table collection add index FK7043CBC637DFAB81 (boek_id), add constraint FK7043CBC637DFAB81 foreign key (boek_id) references books (id);
alter table collection add index FK7043CBC61999E1E (user_id), add constraint FK7043CBC61999E1E foreign key (user_id) references users (id);
Note: I have just translated everything in English, so I might have forgoten a few things.As you see, I have books and users and I want to set up a relation between them. The current situation isn't too bad, but in my Collection table I also want to add the fields price and date_aquired. Unfortunately I have no idea how to create my Hibernate scheme to achieve this.
Code:
create table collection (
book_id integer not null,
user_id integer not null,
price integer,
date_aquired date
);
Now that I'm asking for help: the ideal situation would be to have a User class which has a getBooks() method. This getBooks() method would return a collection of Book objects. Such a Book object would have getISBN(), getTitle() and getYear() getters and would also have a getPrice() and a getDateAquired() method, which would of course be different for every user.
Hope I'm making myself a bit clear here.
I'm afraid this situation is a bit too complicated for me at the moment, so I'd surely appreciate any help I can get!