2

当我在休眠中编写更新查询时。我收到这个错误。我正在使用 Hibernate 3.2 和 MySQL 5.0。在 UserDetails.java 中,Login 是外键引用 Login.java 的主键 (loginId)。

java.lang.NullPointerException
    at org.hibernate.hql.ast.util.SessionFactoryHelper.findSQLFunction(SessionFactoryHelper.java:342)
    at org.hibernate.hql.ast.tree.IdentNode.getDataType(IdentNode.java:266)
    at org.hibernate.hql.ast.tree.BinaryLogicOperatorNode.extractDataType(BinaryLogicOperatorNode.java:168)
    at org.hibernate.hql.ast.tree.BinaryLogicOperatorNode.initialize(BinaryLogicOperatorNode.java:34)
    at org.hibernate.hql.ast.HqlSqlWalker.prepareLogicOperator(HqlSqlWalker.java:1007)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3992)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:1762)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:776)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.updateStatement(HqlSqlBaseWalker.java:358)
    at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:237)
    at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:228)
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)

这是服务类

 Date birthDay = formatter1.parse(brthDay);
        session = HibernateUtil.getSession();
        transaction = session.beginTransaction();
        Login login = (Login) session.get(Login.class, loginId);
        Query query2 = session.createQuery("update UserDetails set name=:Name, secEmailId=:SecEmailId, dob=:DOB, gender=:Gender  where Login=:LoginId");
        query2.setString("Name", name);
        query2.setString("SecEmailId", secEmailId);
        query2.setDate("DOB", birthDay);
        query2.setString("Gender", gender);
        query2.setParameter("LoginId", login);
        profileUpdated = query2.executeUpdate();

这是 UserDetails.java pojo 类

private Long udid;
private Login login;
private String name;
private String secEmailId;
private Date dob;
private String gender;
private long createrId;
private Date createdDate;
private long updaterId;
private Date updatedDate;
settes & getters
4

1 回答 1

0

您的实体中只有登录对象。调用外键必须是(整数或整数)才能放入 HQL。而且您不能将外键称为 login.getId() 因为 hibernate 不允许在 HQL 中使用括号。使用 Integer 的目的是可以将其设置为 null。

private Login login;

您必须引用您的实体,但不想从数据库中加载它而只是为了获取外键,请使用此方法。但是您需要在 @Column 属性中添加 "insertable"=false 和 "updatable"=false 以防止丢失对实体的正确引用。也许,这是您的 UserDetails 类。

@Entity
public class UserDetails {

    //Other attributes, id, phNo, address, etc..

    @Column(name = "full_name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL) //Relationship is up to you.
    @JoinColumn(name = "login_id")
    private Login login; //This is object.

    @Column(name = "login_id", insertable = false, updatable = false)
    private Integer fkLoginId; //Here is your foreign key.

    //getter and setters.
}

这不会在数据库中生成另一列,因为它无法插入或更新,它只是引用了一个列。之后,您将 fkLoginId 作为外键并像这样使用 HQL 更新您的 UserDetails。

String name = "Updated Name";
int id = 1;

session = HibernateUtil.getSession(); //Consider declared
transaction = session.beginTransaction();
Login login = (Login) session.get(Login.class, loginId);
String hql = "update UserDetails set name=:Name where fkLoginId=:Id"; //<---Ans
Query query = session.createQuery(hql);
query.setParameter("Name", name);
query.setParameter("Id", id);
int result = query.executeUpdate();
transaction.commit();
session.close();

希望能帮助到你。

于 2019-07-20T19:19:09.520 回答