JPA(Hibernate)查询返回没有有效查询的结果

时间:2011-12-03 23:11:42

标签: hibernate jpa resultset

环境是:

在W2K8 Datacenter Server上运行的SQL Server 2008,

在Mac OS X 10.6.8上运行: Hibernate(3),JPA Provider(1.0.1-Final), Spring 3.0.6, Java 6

情况是有一个特定的对象我无法检索结果集。即使最简单的查询也不起作用,它似乎仅限于这个特定的Entity类(InvDetail)。我已经记录了生成的SQL:

休眠:

select invdetail0_.invdetailid as invdetai1_28_, invdetail0_.description as                                       descript2_28_, invdetail0_.invid as invid28_, invdetail0_.qty as qty28_,     invdetail0_.unitprice as unitprice28_ from INV_DETAIL invdetail0_`

这是表创建脚本:

CREATE TABLE [dbo].[INV_DETAIL]( [INVID] [int] NOT NULL, [INVDETAILID] [int] NOT NULL, [QTY] [int] NOT NULL, [UNITPRICE] [smallmoney] NOT NULL, [DESCRIPTION] [varchar](100) NOT NULL, CONSTRAINT [PK_INV_DETAIL] PRIMARY KEY CLUSTERED ( [INVDETAILID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY] ) ON [PRIMARY]

Java类本身:

    @Entity
@NamedQueries({
  @NamedQuery(name = "InvDetail.findAll", query = "select o from InvDetail o"),
  @NamedQuery(name = "InvDetail.findByHeaderId", query = "SELECT o from InvDetail o WHERE o.invid = :invid")
})
@Table(name = "INV_DETAIL")
public class InvDetail extends SmartBeanItem<InvDetail> implements Serializable {
  @Column(name ="description", nullable = false)
  private String description;
  @Id
  @Column(name="invdetailid",nullable = false)
  private Integer invdetailid;
  @Column(name="invid",nullable = false)
  private Integer invid;
  @Column(name="qty",nullable = false)
  private Integer qty;
  @Column(name="unitprice",nullable = false)
  private BigDecimal unitprice;

  public InvDetail() {
  }

  public InvDetail(String description, Integer invdetailid, Integer invid,
                   Integer qty, BigDecimal unitprice) {
    this.description = description;
    this.invdetailid = invdetailid;
    this.invid = invid;
    this.qty = qty;
    this.unitprice = unitprice;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  public Integer getInvdetailid() {
    return invdetailid;
  }

  public void setInvdetailid(Integer invdetailid) {
    this.invdetailid = invdetailid;
  }

  public Integer getInvid() {
    return invid;
  }

  public void setInvid(Integer invid) {
    this.invid = invid;
  }

  public Integer getQty() {
    return qty;
  }

  public void setQty(Integer qty) {
    this.qty = qty;
  }

  public BigDecimal getUnitprice() {
    return unitprice;
  }

  public void setUnitprice(BigDecimal unitprice) {
    this.unitprice = unitprice;
  }
}

我的persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
    <persistence-unit name="imUnit" transaction-type="RESOURCE_LOCAL">
        ...
        <class>com.stuff.portal.portlet.model.ejb.InvHeader</class>
        <class>com.stuff.portal.portlet.model.ejb.InvDetail</class>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.jdbc.batch_size" value="100" />
        </properties>
    </persistence-unit>

我尝试过标准查询(我们执行查询的标准方式)但它们不起作用。所以我尝试简化并移动到命名查询,以下示例不返回任何结果:

TypedQuery<InvDetail> q = em.createNamedQuery("InvDetail.findAll", InvDetail.class);
// q.setParameter("invid", invid);
List<InvDetail> result = q.getResultList();

数据在数据库中,当我运行hibernate直接针对数据库生成的SQL时,我得到了我期望的结果。据我所知,日志表明没有错,如果事务或数据库出现问题,我会期望抛出异常。

这是我的日志:

DEBUG [http-8080-6] (JpaTransactionManager.java:355) - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@23b76287]
DEBUG [http-8080-6] (ExtendedEntityManagerCreator.java:423) - Starting resource local transaction on application-managed EntityManager [org.hibernate.ejb.EntityManagerImpl@37975a46]
DEBUG [http-8080-6] (ExtendedEntityManagerCreator.java:400) - Joined local transaction
DEBUG [http-8080-6] (ExtendedEntityManagerCreator.java:400) - Joined local transaction
Hibernate: select invheader0_.invid as invid27_, invheader0_.exchangerate as exchange2_27_, invheader0_.incotermlocation as incoterm3_27_, invheader0_.incotermsid as incoterm4_27_, invheader0_.invcurrencyid as invcurre5_27_, invheader0_.invno as invno27_, invheader0_.invoicedate as invoiced7_27_, invheader0_.orgid as orgid27_, invheader0_.paymentstatusid as payments9_27_, invheader0_.paymenttermid as payment10_27_, invheader0_.paymentterms as payment11_27_, invheader0_.revisionnumber as revisio12_27_, invheader0_.supplierid as supplierid27_, invheader0_.surcharge as surcharge27_, invheader0_.tax as tax27_ from INV_HEADER invheader0_ where 1=1
DEBUG [http-8080-6] (AbstractPlatformTransactionManager.java:752) - Initiating transaction commit
DEBUG [http-8080-6] (JpaTransactionManager.java:462) - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@79ae52b2]
DEBUG [http-8080-6] (JpaTransactionManager.java:548) - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@79ae52b2] after transaction
DEBUG [http-8080-6] (EntityManagerFactoryUtils.java:328) - Closing JPA EntityManager
DEBUG [http-8080-6] (AbstractBeanFactory.java:244) - Returning cached instance of singleton bean 'inventoryManagerTransactionManager'
DEBUG [http-8080-6] (AbstractBeanFactory.java:244) - Returning cached instance of singleton bean 'liferayTransactionManager'
DEBUG [http-8080-6] (AbstractPlatformTransactionManager.java:365) - Creating new transaction with name [com.stuff.portal.portlet.service.impl.InventoryManagerServiceImpl.getDetailsForInvoice]: PROPAGATION_NESTED,ISOLATION_DEFAULT; 'inventoryManagerTransactionManager'
DEBUG [http-8080-6] (JpaTransactionManager.java:323) - Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@50b97081] for JPA transaction
DEBUG [http-8080-6] (JpaTransactionManager.java:355) - Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@3e894e8e]
DEBUG [http-8080-6] (ExtendedEntityManagerCreator.java:423) - Starting resource local transaction on application-managed EntityManager [org.hibernate.ejb.EntityManagerImpl@37975a46]
DEBUG [http-8080-6] (ExtendedEntityManagerCreator.java:400) - Joined local transaction
22:38:51,519 WARN  [ThemeLocalServiceImpl:121] No theme found for specified theme id clear_WAR_cleartheme. Returning the default theme.
22:38:51,521 WARN  [ThemeLocalServiceImpl:121] No theme found for specified theme id clear_WAR_cleartheme. Returning the default theme.
Hibernate: select invdetail0_.invdetailid as invdetai1_28_, invdetail0_.description as descript2_28_, invdetail0_.invid as invid28_, invdetail0_.qty as qty28_, invdetail0_.unitprice as unitprice28_ from INV_DETAIL invdetail0_
DEBUG [http-8080-6] (AbstractPlatformTransactionManager.java:752) - Initiating transaction commit
DEBUG [http-8080-6] (JpaTransactionManager.java:462) - Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@50b97081]
DEBUG [http-8080-6] (JpaTransactionManager.java:548) - Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@50b97081] after transaction
DEBUG [http-8080-6] (EntityManagerFactoryUtils.java:328) - Closing JPA EntityManager

现在有趣的是,如果你看一下日志,就会有一个不同的查询来检索一些InvHeader实体。该查询完全正常,随后我尝试获取InvDetail ......无济于事。这似乎只发生在InvDetail上,系统中的所有其他对象似乎都可以被查询而没有任何问题。

我不知道如何进一步解决这个问题。我已经打开了我认为所有适当的日志记录,并且没有找到任何可以解释为什么在我要求表格中的所有内容时返回0结果的内容。

非常感谢任何协助。

0 个答案:

没有答案