简单的休眠不工作,但级联查询

时间:2016-03-28 04:27:46

标签: mysql hibernate orm

我有一个“transactionitems”表表,其中包含FK到父“transactions”表。查询解决事务行的“transactionitems”表时,我没有遇到任何问题。但是,当我对事务表进行直接查询时,我得不到任何结果。我在控制台中看不到任何错误或异常。

如下所示的方法调用会产生正确的结果

Session s = DataAccess.ObtainSession();
        Criteria cr = s.createCriteria(TransactionItem.class);
        cr.createAlias("transaction", "transaction");
        cr.add(Restrictions.eq("transaction.id", txid));
        List<TransactionItem> results = cr.list();

但是更简单的方法调用返回空列表

 Session s = DataAccess.ObtainSession();
        Criteria cr = s.createCriteria(Transaction.class);
        List<Transaction> list = cr.list();

Hibernate映射文件和类

<?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">
<hibernate-mapping>
    <class name="com.beraben.drycleanpos3.entities.Transaction" table="transactions">
        <id column="id" name="id">
            <generator class="increment"/>
        </id>
        <property name="customerName" column="customer_name"/>
        <property name="customerContact" column="customer_contact"/>
        <property name="createdDate" type="date" column="created_datetime"/>
        <property name="dueDate" type="date" column="due_datetime"/>
        <property name="readyDate" type="date" column="ready_datetime"/>
        <property name="jobClosedDate" type="date" column="job_closed_datetime"/>
        <property name="notes" />
    </class>
</hibernate-mapping>

<?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">
<hibernate-mapping>
    <class name="com.beraben.drycleanpos3.entities.TransactionItem" table="transaction_items">

        <id column="id" name="id">
            <generator class="increment"/>
        </id>
        <many-to-one class="com.beraben.drycleanpos3.entities.Transaction" column="transaction_id" name="transaction" cascade="merge" not-null="true" />
        <property name="garmentTypeName" column="garment_type_name"/>
        <property name="jobTypeName" column="job_type_name"/>
        <property name="price"/>
        <property name="notes"/>

    </class>
</hibernate-mapping>



public class TransactionItem {
    private int id;
    private Transaction transaction;
    private String garmentTypeName;
    private String jobTypeName;
    private int price;
    private String notes;


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Transaction getTransaction() {
        return transaction;
    }

    public void setTransaction(Transaction transaction) {
        this.transaction = transaction;
    }

    public String getGarmentTypeName() {
        return garmentTypeName;
    }

    public void setGarmentTypeName(String garmentTypeName) {
        this.garmentTypeName = garmentTypeName;
    }

    public String getJobTypeName() {
        return jobTypeName;
    }

    public void setJobTypeName(String jobTypeName) {
        this.jobTypeName = jobTypeName;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }



}



public class Transaction {
    private int id;
    private String customerName;
    private String customerContact;

    @JsonSerialize(using = CustomDateSerializer.class)
    private Date createdDate;

    @JsonSerialize(using = CustomDateSerializer.class)
    private Date dueDate;

    @JsonSerialize(using = CustomDateSerializer.class)
    private Date readyDate;

    @JsonSerialize(using = CustomDateSerializer.class)
    private Date jobClosedDate;

    private String notes;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getCustomerContact() {
        return customerContact;
    }

    public void setCustomerContact(String customerContact) {
        this.customerContact = customerContact;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public Date getCreatedDate() {
        return createdDate;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public Date getDueDate() {
        return dueDate;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public void setDueDate(Date dueDate) {
        this.dueDate = dueDate;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public Date getReadyDate() {
        return readyDate;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public void setReadyDate(Date readyDate) {
        this.readyDate = readyDate;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public Date getJobClosedDate() {
        return jobClosedDate;
    }

    @JsonSerialize(using = CustomDateSerializer.class)
    public void setJobClosedDate(Date jobClosedDate) {
        this.jobClosedDate = jobClosedDate;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    @Override
    public String toString() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        return "Transaction{" + "id=" + id + ", customerName=" + customerName + ", customerContact=" + customerContact + ", createdDate=" + sdf.format(createdDate) + ", dueDate=" + sdf.format(dueDate) + ", readyDate=" + readyDate + ", jobClosedDate=" + jobClosedDate + ", notes=" + notes + '}';
    }

1 个答案:

答案 0 :(得分:0)

实际上这些问题应该是对你的问题的评论,但不幸的是我还没有权限。

您的映射显示您已在TransactionItem中映射了Transaction,但没有从Transaction到TransactionItem的链接。在这种情况下,您可以对结果稍微具体一点吗?

  1. 关于Transaction的查询,您是否完全没有结果或者您是说结果中不包含TransactionItem数据?

  2. TransactionItem上执行查询时,您是否看到了Transaction的数据?

  3. 另外,检查Hibernate用于获取数据的最终查询的日志。如果你尝试直接在SQL中执行它,它可能会给出它无法工作的原因。