-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Issue with composite key/hibernate 3.1
PostPosted: Wed Aug 15, 2007 11:44 am 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
Hibernate version: 3.1

Mapping documents:

Transaction Header:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.xpedx.commissions.commission.persist.Transactionheaders" table="transactionheaders" schema="public">
<composite-id name="id" class="com.xpedx.commissions.commission.persist.TransactionheadersId">
<key-property name="store" type="java.lang.String">
<column name="store" length="3" />
</key-property>
<key-property name="date" type="java.util.Date">
<column name="date" length="29" />
</key-property>
<key-property name="ticketnumber" type="java.lang.String">
<column name="ticketnumber" length="6" />
</key-property>
<key-property name="ticketsequence" type="java.lang.String">
<column name="ticketsequence" length="3" />
</key-property>
</composite-id>
<many-to-one name="salesrep" class="com.xpedx.commissions.commission.persist.Salesrep" fetch="select">
<column name="ticketsalesrep" length="10" />
</many-to-one>
<property name="id_1" type="java.lang.Integer">
<column name="id" not-null="true" />
</property>
<property name="custsalesrep" type="java.lang.String">
<column name="custsalesrep" length="10" />
</property>
<property name="salesrepused" type="java.lang.String">
<column name="salesrepused" length="10" />
</property>
<property name="customernumber" type="java.lang.String">
<column name="customernumber" length="12" />
</property>
<property name="customername" type="java.lang.String">
<column name="customername" length="25" />
</property>
<property name="headercommissionamt" type="java.lang.Double">
<column name="headercommissionamt" precision="17" scale="17" />
</property>
<set name="transactionlineses" inverse="true">
<key>
<column name="store" length="3" not-null="true" />
<column name="date" length="29" not-null="true" />
<column name="ticketnumber" length="6" not-null="true" />
<column name="ticketsequence" length="3" not-null="true" />
</key>
<one-to-many class="com.xpedx.commissions.commission.persist.Transactionlines" />
</set>
</class>
</hibernate-mapping>


linte items

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.xpedx.commissions.commission.persist.Transactionlines" table="transactionlines" schema="public">
<composite-id name="id" class="com.xpedx.commissions.commission.persist.TransactionlinesId">
<key-many-to-one name="transactionheaders" class="com.xpedx.commissions.commission.persist.Transactionheaders">
<column name="store" length="3" />
<column name="date" length="29" />
<column name="ticketnumber" length="6" />
<column name="ticketsequence" length="3" />
</key-many-to-one>
<key-property name="recordtype" type="java.lang.String">
<column name="recordtype" length="1" />
</key-property>
<key-property name="linenumber" type="java.lang.String">
<column name="linenumber" length="5" />
</key-property>
<key-property name="processdate" type="java.util.Date">
<column name="processdate" length="29" />
</key-property>
</composite-id>
<property name="itemnumber" type="java.lang.String">
<column name="itemnumber" length="15" />
</property>
<property name="itemdescription1" type="java.lang.String">
<column name="itemdescription1" length="25" />
</property>
<property name="itemdescription2" type="java.lang.String">
<column name="itemdescription2" length="25" />
</property>
<property name="itemprimevendor" type="java.lang.String">
<column name="itemprimevendor" length="12" />
</property>
<property name="primevendorpart" type="java.lang.String">
<column name="primevendorpart" length="15" />
</property>
<property name="itemtype" type="java.lang.String">
<column name="itemtype" length="1" />
</property>
<property name="quantitysold" type="java.lang.Double">
<column name="quantitysold" precision="17" scale="17" />
</property>
<property name="unitofmeasure" type="java.lang.String">
<column name="unitofmeasure" length="5" />
</property>
<property name="linecost" type="java.lang.Double">
<column name="linecost" precision="17" scale="17" />
</property>
<property name="linestdcost" type="java.lang.Double">
<column name="linestdcost" precision="17" scale="17" />
</property>
<property name="linecostused" type="java.lang.Double">
<column name="linecostused" precision="17" scale="17" />
</property>
<property name="netlinesalesprice" type="java.lang.Double">
<column name="netlinesalesprice" precision="17" scale="17" />
</property>
<property name="priceoverridecode" type="java.lang.String">
<column name="priceoverridecode" length="1" />
</property>
<property name="commissioncalcmethod" type="java.lang.String">
<column name="commissioncalcmethod" length="1" />
</property>
<property name="linecommissionamount" type="java.lang.Double">
<column name="linecommissionamount" precision="17" scale="17" />
</property>
<property name="createdby" type="java.lang.String">
<column name="createdby" length="10" />
</property>
</class>
</hibernate-mapping>



Name and version of the database you are using: postgres 8.3

The generated SQL (show_sql=true): select transactio0_.store as store0_0_, transactio0_.date as date0_0_, transactio0_.ticketnumber as ticketnu3_0_0_, transactio0_.ticketsequence as ticketse4_0_0_, transactio0_.ticketsalesrep as ticketsa5_0_0_, transactio0_.id as id0_0_, transactio0_.custsalesrep as custsale7_0_0_, transactio0_.salesrepused as salesrep8_0_0_, transactio0_.customernumber as customer9_0_0_, transactio0_.customername as custome10_0_0_, transactio0_.headercommissionamt as headerc11_0_0_ from public.transactionheaders transactio0_ where transactio0_.store='002' and transactio0_.date='2007-06-01 00:00:00' and transactio0_.ticketnumber='508583' and transactio0_.ticketsequence='001'

I tried this sql and it retrieved data fine from db. But when I tested with code like this,

TransactionheadersId header = new TransactionheadersId();

Calendar cal = Calendar.getInstance();
cal.set(2007, 05, 01, 00, 00, 00);
header.setStore("002");
header.setTicketnumber("508583");
header.setTicketsequence("001");
header.setDate(cal.getTime());
TransactionheadersDAO dao = new TransactionheadersDAO();
Transactionheaders headers = dao.findById(header);


if (headers != null) {
System.out.println("" + headers.getCustomername());
} else {
System.out.println("Headers is null");
}

The headers object is not getting filled. I checked database and found data for this specific set of values.

I am not getting any hibernate specific exceptions.

Can anybody give hint on what is wrong here?

Thanks
Sun


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 1:09 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Almost certainly it'll be an issue with the date field not matching. I suggest running a query on 3 of the 4 fields (exclude the date) and printing the value of the dates returned from the database.

If you're not mapping to a legacy schema I suggest you get rid of the composite key and use a simple surrogate key instead (e.g. a Long). Look how clumsy it is to load an item from the database!


Top
 Profile  
 
 Post subject: Composite primary key
PostPosted: Wed Aug 15, 2007 3:05 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
Thanks for reply.

I tried without date and that also didn't bring any data back.

I have one generic question. In addition to this two tables , I have few more tables all with composite keys.

Mine is not legacy database , Do you suggest not to use composite primary key at all? Instead only surrogate key in all tables?

Thanks
Sundar


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 4:02 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Personally, I would recommend not using a composite key, they end up being more trouble than they're worth IMHO.

Can you show your code from your DAO? If you can see the sql being executed there has to be something amiss in there I would think.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: composite key
PostPosted: Wed Aug 15, 2007 4:30 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.hibernate.Criteria;
import org.hibernate.criterion.Expression;

/**
* Data access object (DAO) for domain model class Transactionheaders.
*
* @see com.xpedx.commissions.commission.persist.Transactionheaders
* @author MyEclipse Persistence Tools
*/

public class TransactionheadersDAO extends BaseHibernateDAO {
private static final Log log = LogFactory
.getLog(TransactionheadersDAO.class);

// property constants
public static final String ID_1 = "id_1";

public static final String CUSTSALESREP = "custsalesrep";

public static final String SALESREPUSED = "salesrepused";

public static final String CUSTOMERNUMBER = "customernumber";

public static final String CUSTOMERNAME = "customername";

public static final String HEADERCOMMISSIONAMT = "headercommissionamt";

public void save(Transactionheaders transientInstance) {
log.debug("saving Transactionheaders instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}

public void delete(Transactionheaders persistentInstance) {
log.debug("deleting Transactionheaders instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}

public Transactionheaders findById(
com.xpedx.commissions.commission.persist.TransactionheadersId id) {
log.debug("getting Transactionheaders instance with id: " + id);
Transactionheaders instance = null;
try {
instance = (Transactionheaders) getSession()
.get(
"com.xpedx.commissions.commission.persist.Transactionheaders",
id);

} catch (HibernateException e){
log.error("Exception" + e.getMessage());
}
return instance;
}

public List findByExample(Transactionheaders instance) {
log.debug("finding Transactionheaders instance by example");
try {
List results = getSession()
.createCriteria(
"com.xpedx.commissions.commission.persist.Transactionheaders")
.add(Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}

public List findByProperty(String propertyName, Object value) {
log.debug("finding Transactionheaders instance with property: "
+ propertyName + ", value: " + value);
try {
String queryString = "from Transactionheaders as model where model."
+ propertyName + "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}

public List getAllHeaders() {

Calendar cal = Calendar.getInstance();
cal.set(2007, 05, 01);

Calendar cal2 = Calendar.getInstance();
cal2.set(2007, 01, 01);




Criteria criteria = getSession().createCriteria(Transactionheaders.class);
criteria.add(Expression.ge("date",cal2.getTime()));
criteria.add(Expression.le("date",cal.getTime()));
List results = criteria.list();


return results;

}



public List findById_1(Object id_1) {
return findByProperty(ID_1, id_1);
}

public List findByCustsalesrep(Object custsalesrep) {
return findByProperty(CUSTSALESREP, custsalesrep);
}

public List findBySalesrepused(Object salesrepused) {
return findByProperty(SALESREPUSED, salesrepused);
}

public List findByCustomernumber(Object customernumber) {
return findByProperty(CUSTOMERNUMBER, customernumber);
}

public List findByCustomername(Object customername) {
return findByProperty(CUSTOMERNAME, customername);
}

public List findByHeadercommissionamt(Object headercommissionamt) {
return findByProperty(HEADERCOMMISSIONAMT, headercommissionamt);
}

public List findAll() {
log.debug("finding all Transactionheaders instances");
try {
String queryString = "from Transactionheaders";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}

public Transactionheaders merge(Transactionheaders detachedInstance) {
log.debug("merging Transactionheaders instance");
try {
Transactionheaders result = (Transactionheaders) getSession()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}

public void attachDirty(Transactionheaders instance) {
log.debug("attaching dirty Transactionheaders instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}

public void attachClean(Transactionheaders instance) {
log.debug("attaching clean Transactionheaders instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
}

generated using myeclipse.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 4:35 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
And it isn't throwing an exception?

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: composite key
PostPosted: Wed Aug 15, 2007 4:43 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
No exception . I got exception message as null


Top
 Profile  
 
 Post subject: composite key
PostPosted: Wed Aug 15, 2007 4:48 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
I removed date from hbm.xml of both header and detail , it worked and brought value of one of the columns i tested.

That means it looks like issue with date value I submitted.

Still exploring.

Sundar


Top
 Profile  
 
 Post subject: Re: composite key
PostPosted: Wed Aug 15, 2007 5:08 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
sundarrajan wrote:
No exception . I got exception message as null


Does that mean there was an exception but just the message was null?

If so, what was the stack trace?

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: composite key
PostPosted: Wed Aug 15, 2007 5:18 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
I didn't see any exception , the exception message was null.


Top
 Profile  
 
 Post subject: Re: composite key
PostPosted: Wed Aug 15, 2007 5:34 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
sundarrajan wrote:
I didn't see any exception , the exception message was null.


I am assuming you are seeing: "Exception null" in your logs?

Change:

Code:
catch (HibernateException e){
log.error("Exception" + e.getMessage());
}


to:

Code:
catch (HibernateException e){
log.error("Exception" + e.getMessage());
throw new RuntimeException(e);
}


And then post the resulting stack trace.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: composite key
PostPosted: Wed Aug 15, 2007 6:16 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
I tried the same way with exception logic , but no exception. Execution crossed the call.

public Transactionheaders findById(
com.xpedx.commissions.commission.persist.TransactionheadersId id) {
Transactionheaders instance = null;
System.out.println("getting Transactionheaders instance with id: " + id);
try {
instance = (Transactionheaders) getSession()
.get(
"com.xpedx.commissions.commission.persist.Transactionheaders",
id);
System.out.println("Crossed execution");
System.out.println(instance.getSalesrepused());
} catch (HibernateException e){
System.out.println("Inside exception");
log.error("Exception" + e.getMessage());
throw new RuntimeException(e);
}
return instance;
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 7:23 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
And that is with the date stuff back in your key? I'm confused as you were saying the exception message was null.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject: composite key
PostPosted: Wed Aug 15, 2007 8:46 pm 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
Yes that is with date stuff in .

There are two exceptions I am handling , one in DAO and one in test class.

It crossed the execution in DAO without any exception , when I tried to retrieve values loaded, it threw message null in my test classl

Another test case tried:
Removed date stuff from hbm.xml and this time it worked and retrived values at one level.

Does it make sense?


Top
 Profile  
 
 Post subject: composite key
PostPosted: Thu Aug 16, 2007 11:53 am 
Beginner
Beginner

Joined: Tue Aug 14, 2007 6:29 pm
Posts: 27
Location: chicago
thatmikewilliams,
If composite key is not best practice with hibernate what would be the case with master-detail table? Don't we need the master table key in addition to the primary key of detail table as composite primary key?

Thanks
Sundar


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.