-1

从表中获取用户 ID 的查询是什么?

表格是:

CREATE TABLE `UPLOADIMAGE` 
(
    `IMAGE_ID` int(11) NOT NULL AUTO_INCREMENT,
    `FILE_DATA` tinyblob,
    `FILE_NAME` varchar(255) DEFAULT NULL,
    `FILE_PATH` varchar(255) DEFAULT NULL,
    `USER_ID` int(11) DEFAULT NULL,
    PRIMARY KEY (`IMAGE_ID`),
    KEY `FKC057C7DA8B4610BF` (`USER_ID`),
    CONSTRAINT `FKC057C7DA8B4610BF` 
        FOREIGN KEY (`USER_ID`) REFERENCES `USER` (`USER_ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

这是我获取用户标识的java代码:

public int showUserImageId(int userid)
{
    String askedQuery="select u.USER_ID from UPLOADIMAGE u where u.USER_ID = :id";
    TypedQuery<UploadImage> query =   
        entityManager.createQuery(askedQuery,UploadImage.class);
    entityManager.getTransaction().begin();
    query.setParameter("id", userid);
    int out= query.getSingleResult().getUser().getUserid();
    entityManager.getTransaction().commit();
    return out;
}

我的 UploadImage 实体是:

  @Entity
  @Table(name="UPLOADIMAGE")
   public class UploadImage
    {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="IMAGE_ID")
private int imageid;

@Column(name="FILE_NAME")
private String fileName;


@Column(name="FILE_PATH")
private String filePath;

@JoinColumn(name="USER_ID")
@OneToOne(cascade= CascadeType.DETACH)
private User user;

@Column(name="FILE_DATA")
private CommonsMultipartFile FileData;
     }

我得到的错误是:

  Exception in thread "main" java.lang.IllegalArgumentException:   org.hibernate.hql.ast.QuerySyntaxException: UPLOADIMAGE is not mapped [select  u.USER_ID from   UPLOADIMAGE u where u.USER_ID = :id]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1222)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1168)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:292)
at se.datalayer.guards.service.ImageUploadService.showUserImageId(ImageUploadService.java:162)
at se.datalayer.guards.main.Main.getUserIdFromImage(Main.java:70)
at se.datalayer.guards.main.Main.main(Main.java:48)
    Caused by: org.hibernate.hql.ast.QuerySyntaxException: UPLOADIMAGE is not mapped [select  u.USER_ID from UPLOADIMAGE u where u.USER_ID = :id]
at  org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:181)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:93)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:315)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3357)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3241)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:726)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:577)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:294)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:237)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:254)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:98)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1760)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:277)
... 3 more

谢谢你的帮助

4

1 回答 1

0

JPQL 不使用表和列。它使用 JPA 实体及其持久字段。因此,要使用 JPQL 查询,您需要一个 JPA 实体UploadImage,映射到您的表,并包含持久字段(imageId、userId、filePath 等)。

然后查询看起来像

select u.userId from UploadImage u where u.userId = :userId

JPA 是一个 ORM:对象关系映射器。重点是能够使用对象而不是关系数据。

也就是说,我并没有真正看到使用此用户 ID 作为参数从数据库中获取用户 ID 的意义。

于 2012-12-02T23:08:27.210 回答