简体   繁体   中英

Simple hibernate not working but cascaded queries are

I have a "transactionitems" table tables which contain FK to a parent "transactions" table. I have not problems querying the "transactionitems" tables which resolve the transaction rows. However when i do a direct query to the transaction table i get no results back. I cannot see any errors or exceptions in the console out.

A method call like the following produces the correct results

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();

But a simpler method call return empty list

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

Hibernate mapping files and classes below

<?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 + '}';
    }

Actually these questions should have gone as comments to your question, but unfortunately I do not have permissions yet.

Your mapping shows that you have mapped Transaction in TransactionItem but no link from Transaction to TransactionItem. In this context, can you be little more specific about the results?

  1. On your query on Transaction , are you getting absolutely no results OR are you saying that TransactionItem data is not included in the result?

  2. When you execute query on TransactionItem , do you see the data of Transaction ?

Also, check the log on the final query used by Hibernate to fetch the data. It may give an idea on why it is not working, if you try executing it directly in SQL.

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