-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: static lists
PostPosted: Tue Aug 08, 2006 12:05 pm 
Beginner
Beginner

Joined: Tue Aug 08, 2006 11:53 am
Posts: 37
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 08, 2006 11:17 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Your options are to
  • map the many-to-one from contract to contracttype with cascade=none, so that only the contracttype id is written to the contract row in the DB and nothing is ever written to the contracttype table, or
  • to create a contracttype enum that mirrors the database table. This is slightly more DB-efficient (as it means that hibernate will never even access the contracttypes table), but it adds some overhead in that when you do add a new contracttype to the DB, you'll have to update your code and redeploy your application.
Have a look at the wiki section of http://www.hibernate.org/37.html, there are some articles on enums, usertypes and lookup takes in there.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject: Read then set.
PostPosted: Wed Aug 09, 2006 9:32 am 
Newbie

Joined: Mon Sep 26, 2005 11:10 am
Posts: 5
You may want to read the existing contract_types from the db, and then use one of those to set on the contract. This way you are associating an existing one, not creating a new one.

So it would look something like this:

Code:
Session ses=sFactory.openSession();
ContractType ct = ses.find(ContractType.class, 1);
// Or you could create a query to do something like findByName("CSA Lite");
Contract co=new Contract();
co.setDate(new Date());
co.setContractType(ct);
//ses.save(ct); Don't need to save this because it already exists.
ses.saveOrUpdate(co);
ses.flush();


Hope this helps.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.