I have an existing database relationship which I'm trying to convert to Hibernate relationships. This is in brief how my relatioships are,
The Database tables
Code:
CREATE TABLE Author (
BOOK_ID VARCHAR2 (1) NOT NULL,
BOOK_NAME VARCHAR2 (2) NOT NULL,
PRIMARY KEY ( BOOK_ID)) ;
CREATE TABLE Person (
PERSON_ID VARCHAR2 (1) NOT NULL,
PERSON_NAME VARCHAR2 (2) NOT NULL,
PRIMARY KEY ( PERSON_ID)) ;
CREATE TABLE Academics (
ACADEMY_ID VARCHAR2 (1) NOT NULL,
ACADEMY_NAME VARCHAR2 (2) NOT NULL,
PRIMARY KEY ( ACADEMIC_ID)) ;
// Many to Many Relationship
CREATE TABLE WORKS (
BOOK_ID VARCHAR2 (1) NOT NULL,
PERSON_ID VARCHAR2 (1) NOT NULL,
ACADEMY_ID VARCHAR2 (1) NOT NULL,
PRIMARY KEY ( BOOK_ID, PERSON_ID, ACADEMY_ID) ) ;
The Objects
Code:
public class Author{
private String bookId;
private String bookName;
private Set workSet; // Initialized with all Works Objects at startup.
}
public class Person{
private String bookId;
private String bookName;
}
public class Academics{
private String academyId;
private String academyName;
}
public class Works{
private String bookId;
private String academyId;
private String personId;
}
The Mappings
Code:
<hibernate-mapping>
<class name="Works" table="Works">
<composite-id>
<key-property column="BOOK_ID" length="1" name="bookId" type="java.lang.String"/>
<key-property column="PERSON_ID" length="1" name="personId" type="java.lang.String"/>
<key-property column="ACADEMY_ID" length="1" name="academyId" type="java.lang.String"/>
</composite-id>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="Author" table="AUTHOR">
<id column="AUTHOR_ID" name="authorId" type="java.lang.String">
<generator class="assigned"/>
</id>
<set name="workSet" table="WORKS" inverse="true" lazy="false" cascade="none" sort="unsorted">
<key column="AUTHOR_ID" />
<one-to-many class="Works" />
</set>
</class>
</hibernate-mapping>
Now my questions,
1. Is there any way that I can specify the classes Book, Person, Academy as the composite keys in the Works Mapping.
2. Upon a search from the database, I get a list of Author Objects which has a list of Works Objects which could have
duplicate entries for Person, Academy. Is there any way to retreive only the distinct entries for Person and Academy
Objects (I can programatically retreive them in the Author class but is there a better way) ?
The Catch :
- The database is preset and I have no flexibility to change it. So whatever I do appears as a workaround as opposed to
fixing the domain model.
Thanks for looking and any thoughts on this would be appreciated.