-->
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.  [ 7 posts ] 
Author Message
 Post subject: Question about multiple primary keys in one table
PostPosted: Tue Jun 21, 2005 9:09 am 
Beginner
Beginner

Joined: Wed May 18, 2005 9:48 am
Posts: 31
Hi all,

I have one table in postgresql with four primary keys. I want to know how to do a select with this four keys. My code is like this:

tTickets.hbm.xml

Code:
<hibernate-mapping package="bbdd.modelos">
<class name="tTickets"
      table="tickets" lazy="true">


        <composite-id>
        <key-property name="Tienda" column="tienda" type="string"/>
        <key-property name="Num_Ticket" column="num_ticket" type="string"/>
        <key-property name="TPV" column="tpv" type="string"/>
        <key-property name="Fecha" column="fecha" type="date"/>
        </composite-id>


   <property   name="Ticket"
            type="string"
            column="ticket"
            not-null="true"/>

</class>
</hibernate-mapping>


tTickets.java

Code:
package bbdd.modelos;

import java.util.Date;
import java.io.Serializable;

/**
*
* @author Administrador
*/
public class tTickets implements Serializable {
        private String sTienda;
        private String sNum_Ticket;
        private String sTPV;
        private Date dFecha;
        private String sTicket;
    /** Creates a new instance of tTickets */
    tTickets() { }
   
    public tTickets(String sTienda, String sNum_Ticket, String sTPV, Date dFecha, String sTicket) {
      this.sTienda = sTienda;
                this.sNum_Ticket = sNum_Ticket;
                this.sTPV = sTPV;
                this.dFecha = dFecha;
                this.sTicket = sTicket;
   }
   
   
    public String getTienda() { return sTienda; }
   
    public String getNum_Ticket() { return sNum_Ticket; }
   
    public String getTPV() { return sTPV; }
   
    public Date getFecha() { return dFecha; }
   
    public String getTicket() { return sTicket; }
   
    public void setTienda(String sTienda) { this.sTienda = sTienda; }
   
    public void setNum_Ticket(String sNum_Ticket) { this.sNum_Ticket = sNum_Ticket; }
   
    public void setTPV(String sTPV) { this.sTPV = sTPV; }
   
    public void setFecha(Date dFecha) { this.dFecha = dFecha; }
   
    public void setTicket(String sTicket) { this.sTicket = sTicket; }
   
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof tTickets)) return false;

        final tTickets tickets = (tTickets) o;

        if (!sTienda.equals(tickets.sTienda)) return false;
        if (!sTPV.equals(tickets.sTPV)) return false;
        if (!sNum_Ticket.equals(tickets.sNum_Ticket)) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = sTienda.hashCode();
        result = 29 * result + sTPV.hashCode();
        result = 29 * result + sNum_Ticket.hashCode();
        return result;
    }
}


TicketsDAO.java

Code:
package bbdd.dao;

import org.hibernate.*;
import exceptions.bbddExceptions;
import bbdd.modelos.tTickets;
import bbdd.oConexion;
import java.io.Serializable;

import java.util.Collection;


public class TicketsDAO {

   public TicketsDAO() {
      oConexion.beginTransaction();
   }

   // ********************************************************** //

   public tTickets getTicket(tTickets ticket, boolean lock)
         throws bbddExceptions {
      Session session = oConexion.getSession();
      tTickets tickets = null;
      try {
         if (lock) {
            tickets = (tTickets) session.load(tTickets.class, ticket , LockMode.UPGRADE);
         } else {
            tickets = (tTickets) session.load(tTickets.class, ticket);
         }
      }  catch (HibernateException ex) {
         throw new bbddExceptions(ex);
      }
      return tickets;
   }

   public Collection findAll()
         throws bbddExceptions {

      Collection items;
      try {
         items = oConexion.getSession().createCriteria(tTickets.class).list();
      } catch (HibernateException ex) {
         throw new bbddExceptions(ex);
      }
      return items;
   }


   // ********************************************************** //

   public void save(tTickets tickets)
         throws bbddExceptions {

      try {
         oConexion.getSession().saveOrUpdate(tickets);
      } catch (HibernateException ex) {
         throw new bbddExceptions(ex);
      }
   }

   // ********************************************************** //

   public void delete(tTickets tickets)
         throws bbddExceptions {

      try {
         oConexion.getSession().delete(tickets);
      } catch (HibernateException ex) {
         throw new bbddExceptions(ex);
      }
   }
}


Is all right? Or there is any problem?. In this code i want that in select you might pass to postgresql four parameters (In one tTicket Object) and make query, but It doesn't work.

Please Help!!!!

Thanks a lot.[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 9:48 am 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
Use HQL or the Criteria API. Session.load is only meant for tables with single-column primary keys.


Top
 Profile  
 
 Post subject: Criteria vs session.load for composite keys
PostPosted: Mon Jul 04, 2005 8:45 am 
Newbie

Joined: Tue Jan 18, 2005 12:21 am
Posts: 2
Location: New Delhi, India
nathanmoon wrote:
Use HQL or the Criteria API. Session.load is only meant for tables with single-column primary keys.


Is this true ? Cause i have cases where i need to load an object based on its composite PK (which contains domain objects). But along with the main object, i also need to load the complete objects in the PK (not just the identifier attributes). e.g.

CompanyProduct - PK contains Company (Company Id) and Product (Product Id) objects.

I have the company id and product id and need to load CompanyProduct along with the "complete" Company and Product objects.

what is the recommended way to go about doing this ?

Also if i need to add restrictions (criteria) on the other (non-identifier) attributes of Company and Product while doing a criteria on CompanyProduct .. how can i achieve this ? e.g.

session.createCriteria(CompanyProduct.class)
.add(Expression.eq(<some condition on Company Name>));

Pls help ...

_________________
Regards,
Nishant


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 4:46 pm 
Beginner
Beginner

Joined: Mon May 02, 2005 6:17 pm
Posts: 41
It is VERY possible to use session.load() on mapped class with composite key.
I had a separate class for composite key whose instance i passed to load method as if it were simple non-composite key. All you have to do is instantiate it and assign values to its keys.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 4:50 pm 
Beginner
Beginner

Joined: Wed May 18, 2005 9:48 am
Posts: 31
mmm, looks interesting; care to share?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 04, 2005 4:59 pm 
Beginner
Beginner

Joined: Mon May 02, 2005 6:17 pm
Posts: 41
off course i am. give me your email or even better write me to borisjns@yahoo.co.uk if you want, and ill send you some very simple code example. But its already explained in hibernate documentation. chapter 8.4 if im not mistaken. cheers


Top
 Profile  
 
 Post subject: session.load vs criteria
PostPosted: Tue Jul 05, 2005 12:15 am 
Newbie

Joined: Tue Jan 18, 2005 12:21 am
Posts: 2
Location: New Delhi, India
i understand it CAN be done. but pls refer to my example ...

i need to load a CompanyProduct object that has a composite PK. The PK is made up of Company object and Product Object - referenced thru the companyId and productId attributes.

i want that :

a) when i load the CompanyProduct object - i also get the completely loaded Company and Product objects. can session.load achieve this ?

b) i can apply restriction criteria on OTHER attributes of Company and Product (apart from companyId and productId) in order to load the CompanyProduct object. can Criteria API achicve this ?

need to know whether (and how) this can be done ... URGENT. am in the middle of delivery ... ;-)

thanks ...

_________________
Regards,
Nishant


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.