我正在尝试做一个hibernate连接 - 查询似乎有效,但是当我尝试从返回到该类型的对象进行强制转换时,我希望它不起作用...我假设因为它也有连接表信息..
@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;
}
我在这里找到了一对一的映射示例 - link
当我从休眠状态恢复对象时,我尝试将其转换为PSNG_SMRY并且它不会工作 - 我如何进行连接,我从TRN表中获取PSNG_SMRY信息并使用连接返回TRN_SYMB?
修改
我收到了无效的强制转换异常 - [Ljava.lang.Object; cannot be cast to PSNG_SMRY
查询代码:
//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;
}
修改: 这是连接类 - 它只用于获取会话信息并执行所有hibernate内容,例如获取数据......
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;
}
}
答案 0 :(得分:0)
我最终搞清楚这一点 - 我收到播放错误的原因是hibernate将PSNG_SMRY
和TRN
对象作为Object[]
返回 - 而不是作为一个对象
答案 1 :(得分:0)
如果你想要PSNG_SMRY实例,你不应该要求TRN表。当您使用JPA映射
时,将为您提供此选项 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
如果您没有获取检索到的PSNG_SMRY对象的TRN,那么这意味着存在映射错误,因为您告诉Hibernate如何在注释中检索PSNG_SMRY的TRN
@OneToOne(mappedBy="smry", cascade=CascadeType.ALL)
public TRN trn;