File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes 1-many (static list) Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "1-many (static list) " Watch "1-many (static list) " New topic
Author

1-many (static list)

aravind yarram
Greenhorn

Joined: Mar 28, 2003
Posts: 12
Hi all,

Here are my relationships
A CONTRACT is always assigned one and only one CONTRACT_TYPE (CSA Lite, PM)

A CONTRACT_TYPE is always associated with zero or more CONTRACTs

The CONTRACT_TYPE is a static list which will not change overtime. The problem I am facing is that I doesnt want to SAVE the Contract_Type for every contract the user enters. however I just want to verify that the contract type user has selected is in the list of contracttypes in the database. I am not sure how to model this. Any help is appreciated.

Thank you in advance!

Hibernate version: 3.0

Mapping documents:

<?xml version="1.0"?>
<hibernate-mapping package="com.dds.domain">

<class name="Contract">
<id name="id" column="ID">
<generator class="sequence">
<param name="sequence">HIBERNATE_SEQUENCE</param>
</generator>
</id>

<property name="date" column="DATE_SUBMITTED" type="date"/>

<many-to-one name="contractType" column="FK_CONTRACT_TYPE_ID" class="ContractType" />

</class>

</hibernate-mapping>
--------------------------------------------------------------------------------------

<?xml version="1.0"?>
<hibernate-mapping package="com.dds.domain">
<class name="ContractType" table="CONTRACT_TYPE">
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">HIBERNATE_SEQUENCE</param>
</generator>
</id>
</class>
</hibernate-mapping>

Contract and ContractType classes

public class Contract implements Serializable
{

private static final long serialVersionUID = -4266076966823864842L;
private long id;
private Date date;
private ContractType contractType;

public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public ContractType getContractType() {
return contractType;
}
public void setContractType(ContractType type) {
this.contractType = type;
}
}


public class ContractType implements Serializable {

private static final long serialVersionUID = 1L;
private long id;
private String name;

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Code between sessionFactory.openSession() and session.close():

Session ses=sFactory.openSession();
ContractType ct=new ContractType();
Contract co=new Contract();
ct.setName("CSA Lite");
co.setDate(new Date());
co.setContractType(ct);
ses.save(ct);
ses.save(co);
ses.flush();

Name and version of the database you are using: Oracle 10g


SCJP2<br />BEA Certified J2EE Developer
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
There does not need to be a mapping for the static list. If you can make sure that the CONTRACT_TYPE table only contains unique entries:

Contract has a many-to-one with ContractType (FK will end up in the CONTRACT table)

when you create a new Contract just select all ContractTypes and check if the new Contract has a type that exists in the list.


pascal
 
 
subject: 1-many (static list)
 
Threads others viewed
why my oracle sequence id increases by 2 when I use hibernate to insert
Hibernate: using oracle sequence
Hibernate Issue
how can i join two different table
hibernate class A containing a list of B
IntelliJ Java IDE

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture