注册

user语法分析错误/无效的表或视图名

DM_878209 2022/11/24 2051 3

为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:8
【操作系统】:windows
【CPU】:
【问题描述】*:
系统使用Hibernate4.3.5,其中类存在外键, 多对一关系,
public class User{
@Id
@Column(name=“ID”)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;

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

@ManyToOne
@JoinColumn(name="Main_Account_Type_ID")
private AccountType mainAccountType;
...

}

public class AccountType{
@Id
@Column(name=“ID”)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;

@Column(name="Name")
private String name;
...

}

第一种方式:

session.get(User.class, new Long(id));

会出现提示user无效的问题:
Console打印出来的执行语句如下
Hibernate:
select
user0_.ID as ID1_20_0_,
user0_.Email as Email5_20_0_,
user0_.Main_Account_Type_ID as Main_Ac22_20_0_,
user0_.Name as Name9_20_0_,
accounttyp1_.ID as ID1_5_1_,
accounttyp1_.Description as Descript2_5_1_,
accounttyp1_.IsInUse as IsInUse3_5_1_,
accounttyp1_.Name as Name4_5_1_
from
user user0_
left outer join
account_type accounttyp1_
on user0_.Main_Account_Type_ID=accounttyp1_.ID
where
user0_.ID=?
WARN : org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: -2007, SQLState: 42000
ERROR: org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 第 1 行, 第 981 列[user]附近出现错误:
语法分析出错
INFO : org.hibernate.event.internal.DefaultLoadEventListener - HHH000327: Error performing load command : org.hibernate.exception.SQLGrammarException: could not prepare statement
ERROR: com.macrosoftsys.urs.controller.UserController - getUser failed, Exception Details=org.hibernate.exception.SQLGrammarException: could not prepare statement

第二种方式:采用sql方式,
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append(“select userInfo.,accountType. from “SYSDBA”.“user” userInfo, “SYSDBA”.“account_type” accountType where userInfo.“ID”= :ID and userInfo.“Main_Account_Type_ID”= accountType.“ID””);
SQLQuery query = session.createSQLQuery(sqlBuilder.toString());
query.setParameter(“ID”,id);
query.addEntity(“userInfo”, User.class);
query.addJoin( “accountType” , “userInfo.mainAccountType”);

总是提示account_type这个表无效。

想问一下像我们这样的需求,应该怎样使用达梦数据库,以及Hibernate才能达到预期目标。

而且user与accounttype表(及java类)中都存在id, name等相同的字段,怎么才能映射到相关的对象中。

回答 0
暂无回答
扫一扫
联系客服