对象名称“ACCOUNT_orders”无效? Spring-MVC Hibernate 带注解的问题
我是休眠新手。我在数据库中有订单和帐户表,
CREATE TABLE [dbo].[ORDERS](
[PRICE] [decimal](12, 2) NULL,
[ORDERID] [int] IDENTITY(0,1) NOT NULL,
[ACCOUNT_ACCOUNTID] [int] NULL,
CONSTRAINT [PK_ORDER] PRIMARY KEY CLUSTERED
(
[ORDERID] ASC
)WITH ( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF )
)
帐户表:
CREATE TABLE [dbo].[ACCOUNT](
[BALANCE] [decimal](12, 2) NULL,
[ACCOUNTID] [int] IDENTITY(0,1) NOT NULL,
[PROFILE_USERID] [varchar](250) NULL,
CONSTRAINT [PK_ACCOUNT] PRIMARY KEY CLUSTERED
(
[ACCOUNTID] ASC
)WITH ( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF )
)
我正在尝试进行一对多映射,一个帐户有许多订单。我有以下类:
@Table(name="ACCOUNT")
public class Account implements Serializable, IEntity{
private static final long serialVersionUID = 1L;
private int accountID;
private double balance;
private List<Order> orders;
@Id
@GeneratedValue
public int getAccountID() {
return accountID;
}
public void setAccountID(int accountID) {
this.accountID = accountID;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "ACCOUNT_ACCOUNTID",nullable=false,insertable=true,updatable=true)
public List<Order> getOrders()
{
return orders;
}
public void setOrders(List<Order> orders)
{
this.orders = orders;
}
}
Order 类:
@Table(name="ORDERS")
public class Order implements Serializable, IEntity
{
private int orderID;
private double price;
@Column(name="ACCOUNT_ACCOUNTID")
private Account account;
@Id
@GeneratedValue
public int getOrderID() {
return orderID;
}
public void setOrderID(int orderID) {
this.orderID = orderID;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
但是当我尝试使用以下 HQL 访问 HibernateTemplate 的 List 方法时:
SQL_QUERY =" from Account as a where a.balance='"+bal+"'";
它给出了以下例外:
(JDBCExceptionReporter.java:72) - Invalid object name 'ACCOUNT_orders'.
java.lang.NullPointerException
at com.microsoft.trade.service.TradeServiceImplementor.loginVerify(TradeServiceImplementor.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
请帮助我。提前致谢。
I am new to Hibernate. I have Order and Account table in DB,
CREATE TABLE [dbo].[ORDERS](
[PRICE] [decimal](12, 2) NULL,
[ORDERID] [int] IDENTITY(0,1) NOT NULL,
[ACCOUNT_ACCOUNTID] [int] NULL,
CONSTRAINT [PK_ORDER] PRIMARY KEY CLUSTERED
(
[ORDERID] ASC
)WITH ( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF )
)
Account Table:
CREATE TABLE [dbo].[ACCOUNT](
[BALANCE] [decimal](12, 2) NULL,
[ACCOUNTID] [int] IDENTITY(0,1) NOT NULL,
[PROFILE_USERID] [varchar](250) NULL,
CONSTRAINT [PK_ACCOUNT] PRIMARY KEY CLUSTERED
(
[ACCOUNTID] ASC
)WITH ( STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF )
)
I am trying to do one to many mapping an One account has many orders. I have following classes:
@Table(name="ACCOUNT")
public class Account implements Serializable, IEntity{
private static final long serialVersionUID = 1L;
private int accountID;
private double balance;
private List<Order> orders;
@Id
@GeneratedValue
public int getAccountID() {
return accountID;
}
public void setAccountID(int accountID) {
this.accountID = accountID;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinColumn(name = "ACCOUNT_ACCOUNTID",nullable=false,insertable=true,updatable=true)
public List<Order> getOrders()
{
return orders;
}
public void setOrders(List<Order> orders)
{
this.orders = orders;
}
}
Order Class:
@Table(name="ORDERS")
public class Order implements Serializable, IEntity
{
private int orderID;
private double price;
@Column(name="ACCOUNT_ACCOUNTID")
private Account account;
@Id
@GeneratedValue
public int getOrderID() {
return orderID;
}
public void setOrderID(int orderID) {
this.orderID = orderID;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@ManyToOne(cascade = CascadeType.ALL ,fetch = FetchType.EAGER)
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
But when I try to access List method of HibernateTemplate with following HQL:
SQL_QUERY =" from Account as a where a.balance='"+bal+"'";
It gives me following ecxeption:
(JDBCExceptionReporter.java:72) - Invalid object name 'ACCOUNT_orders'.
java.lang.NullPointerException
at com.microsoft.trade.service.TradeServiceImplementor.loginVerify(TradeServiceImplementor.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
Help me please. Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有一个双向的 OneToMany 关联:一个账户有多个订单,一个订单有一个账户。其中一种关联与另一种关联相反。因此,您必须在一侧(通常是多侧:这是 JPA 强制执行的)定义此关联的映射,并声明该关联与另一侧(一侧)的另一个关联相反:
请注意,大多数目前,在关系的多方上进行级联是错误的。您不希望订单下达后帐户被删除,是吗?账户持有订单,而不是相反。
最后,关于您的查询:它不是 SQL 查询,而是 HQL 查询。而且你不应该使用连接,而应该使用绑定参数:这更安全(无注入攻击),更清晰(无需引用,更具可读性),并且更快:
You have a bidirectional OneToMany association here : an account has many orders, and an order has one account. One of the association is the inverse of the other one. You must thus define the mapping for this association on one side (usually, the many side: this is mandated by JPA), and declare that the association is the inverse of another one on the other side (the one side) :
Note that most of the time, it's wrong to have a cascade all on the many side of a relationship. You don't want an account to be deleted when an order is, do you? The account holds the orders, not the reverse.
Finally, regarding your query: it's not a SQL query, it's a HQL query. And you should not use concatenation, but bind parameters : this is safer (no injection attack), clearer (no quoting necessary, more readable), and faster :