无法使用请求的结果类型[vinhnguyen.entity.Items]为具有多个返回的查询创建TypedQuery

时间:2018-04-05 09:23:50

标签: java spring spring-boot

我正在尝试编写查询来获取具有特定属性的数据,这里是DAO类

@Repository
@Transactional
public class ItemDAO { 
public static final String BASE_SQL //
= "Select i.id, i.users.id, i.title,i.image,i.descreption,i.reserveprice,i.bidding_startprice,i.bidding_increments,i.start_date,i.itemStatus.id from Items i   ";
@Autowired
private EntityManager entityManager;

public List<Items> findApproveItem() {
    try {
        String sql = BASE_SQL  + " Where i.itemStatus.id = 1 ";

        Query query = entityManager.createQuery(sql, Items.class);
        //query.setParameter("itemStatus", itemStatus);

        return  query.getResultList();
    } catch (NoResultException e) {
        return null;
    }
}

}

这是我的实体类

 @Entity
 @Table(name = "Items", //
    uniqueConstraints = { //
            @UniqueConstraint(name = "ITEM_UK", columnNames = { "u_id", 
"st_id" }) })
public class Items {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "i_id", nullable = false)
private int id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "u_id", nullable = false)
private Users users;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "st_id", nullable = false)
private ItemStatus itemStatus;

@Column(name = "title", nullable = false )
private String title;

@Column(name = "descreption", nullable = false)
private String descreption;

@Lob
@Column(name = "image", length = Integer.MAX_VALUE, nullable = false)
private byte[] image;

@Column(name = "reserveprice", nullable = false)
private Long reserveprice;

@Column(name = "bidding_startprice", nullable = false)
private Long bidding_startprice;

@Column(name = "bidding_increments", nullable = false)
private Long bidding_increments;

@Column(name = "start_date", nullable = false)
@DateTimeFormat(pattern="MM/dd/yyyy hh:mm:ss") 
private Date start_date;


public int getId() {
    return id;
}

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

public Users getUsers() {
    return users;
}

public void setUsers(Users users) {
    this.users = users;
}

public ItemStatus getItemStatus() {
    return itemStatus;
}

public void setItemStatus(ItemStatus itemStatus) {
    this.itemStatus = itemStatus;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescreption() {
    return descreption;
}

public void setDescreption(String descreption) {
    this.descreption = descreption;
}

public byte[] getImage() {
    return image;
}

public void setImage(byte[] image) {
    this.image = image;
}

public Long getReserveprice() {
    return reserveprice;
}

public void setReserveprice(Long reserveprice) {
    this.reserveprice = reserveprice;
}

public Long getBidding_startprice() {
    return bidding_startprice;
}

public void setBidding_startprice(Long bidding_startprice) {
    this.bidding_startprice = bidding_startprice;
}

public Long getBidding_increments() {
    return bidding_increments;
}

public void setBidding_increments(Long bidding_increments) {
    this.bidding_increments = bidding_increments;
}

public Date getStart_date() {
    return start_date;
}

public void setStart_date(Date start_date) {
    this.start_date = start_date;
}

}

这是错误

Caused by: java.lang.IllegalArgumentException: Cannot create TypedQuery for query with more than one return using requested result type [vinhnguyen.entity.Items]
at org.hibernate.internal.AbstractSharedSessionContract.resultClassChecking(AbstractSharedSessionContract.java:740) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:687) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162]
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:350) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at com.sun.proxy.$Proxy103.createQuery(Unknown Source) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162]
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:304) ~[spring-orm-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at com.sun.proxy.$Proxy103.createQuery(Unknown Source) ~[na:na]
at vinhnguyen.dao.ItemDAO.findApproveItem(ItemDAO.java:29) ~[classes/:na]
at vinhnguyen.dao.ItemDAO$$FastClassBySpringCGLIB$$45e97d08.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:747) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.4.RELEASE.jar:5.0.4.RELEASE]
... 96 common frames omitted

我不知道我的查询有什么问题,我是java spring的新手 因为这个页面要求我添加更多细节,所以我为他们添加一些东西让我发布这个,请忽略那里的文字

0 个答案:

没有答案