简体   繁体   中英

hibernate join using an

I am trying to do a hibernate join - the query seemingly works but when i try to cast from the object returned into the type i want it to be it doesnt work...im assuming because it has the joined table info too..

@Entity
@Table(name = "PSNG_SMRY")
public class PSNG_SMRY implements Serializable, Comparable<PSNG_SMRY>
{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(generator="increment")
    @GenericGenerator(name="increment"  , strategy = "increment")
    @Printable
    public Integer SMRY_ID;
    public Integer DEV_ID;
    public Integer RPTD_TRN_ID;

        @OneToOne(mappedBy="smry", cascade=CascadeType.ALL)
        public TRN trn;

}

@Entity
@Table(name = "TRN")
public class TRN implements Serializable
{
    private static final long serialVersionUID = 1L;

    @Id
    public Integer TRN_ID;
        public String TRN_SCTN
        public String TRN_SYMB;

         @OneToOne
        @PrimaryKeyJoinColumn
        private PSNG_SMRY smry;
}

I found this one to one mapping example here - link

And when I get the Object back from hibernate I try to cast it to PSNG_SMRY and it wont work - how am i am to do a join where i get the PSNG_SMRY info and the TRN_SYMB from the TRN table back using a join?

EDIT:

I get an invalid cast exception - [Ljava.lang.Object; cannot be cast to PSNG_SMRY [Ljava.lang.Object; cannot be cast to PSNG_SMRY

query code:

//code  from some function that sets up all queries
String qQuery = "FROM PSNG_SMRY P, TRN T WHERE  T.TRN_ID = P.RPTD_TRN_ID and P.FIR_AXLE_PASD_DT > sysdate - :timeLimit and P.FIR_AXLE_PASD_DT < sysdate - 1/24 ORDER BY P.FIR_AXLE_PASD_DT";

hqlParamList.add(new HQLParams("timeLimit", timeLimit)); //some list to pass to hibernate and then parameterize the queury

result = queryDatabase(qQuery, q4Query, hqlParamList);



public QueryResult queryDatabase(String qQuery, String q4Query,
        List<HQLParams> params) {
    QueryResult results = new QueryResult();

    jwdsqa = new Connection("JWDSQA");
    jwds4qa = new Connection("JWDS4QA");

    results.qa = jwdsqa.retrieve(qQuery, params);
    results.qa4 = jwds4qa.retrieve(q4Query, params);

    return results;
}

EDIT: This is the connection class - it is just used to get the session information and do all the hibernate stuff such as getting data...

public class Connection {

public static Logger logger = Logger.getLogger(Connection.class);

   Session session;
   String sessionName;

public Connection(String name){
session = HibernateUtil.getSessionFactory(name).openSession();

sessionName = name;
    if(session.isConnected()){
        //System.out.println(name + " - Connected");
    }
}

public Session getSession(){
    return session;
}

@SuppressWarnings("unchecked")
public List<Object> retrieve(String qry, List<HQLParams> paramList)
{
    Transaction transaction = null;
    List<Object> obj = null;

    try {
        transaction = session.beginTransaction();

        String queryString = qry;

        Query query = session.createQuery(queryString);

        if(paramList != null)
        {
            for(HQLParams param: paramList)
            {
                query.setParameter(param.paramName, param.params);
            }
        }


        List<Object> obj_ = query.list();
        obj = obj_;
        //session.getTransaction().commit();

    } catch (HibernateException ex) {
        ex.printStackTrace();
        logger.error(ex.getMessage() + "\n" + ex.getStackTrace());
        transaction.rollback();
    } catch (Exception ex) {
        System.err.println(ex.getMessage());
        logger.error(ex.getMessage() + "\n" + ex.getStackTrace());
    }
    finally
    {
        session.close();
        //System.out.println("Closing session " + sessionName);
    }

    return obj;
}
}

我最终弄清楚了这一点-我得到转换错误的原因是休眠的原因是将PSNG_SMRYTRN对象都作为Object[]而不是作为一个Object返回。

If you want PSNG_SMRY instances you should not have to ask for the TRN table. This is provided for you when you use using JPA mapping

   FROM PSNG_SMRY P 
        WHERE P.FIR_AXLE_PASD_DT > sysdate - :timeLimit 
        and P.FIR_AXLE_PASD_DT < sysdate - 1/24 
        ORDER BY P.FIR_AXLE_PASD_DT

If you do not get the TRN for the retrieved PSNG_SMRY objects then it means there is a mapping error because you are telling Hibernate how to retrieve the TRN for a PSNG_SMRY in your annotation

    @OneToOne(mappedBy="smry", cascade=CascadeType.ALL)
    public TRN trn;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM