对象名称“ACCOUNT_orders”无效? Spring-MVC Hibernate 带注解的问题

发布于 2024-10-26 23:24:12 字数 2812 浏览 1 评论 0原文

我是休眠新手。我在数据库中有订单和帐户表,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-11-02 23:24:12

这里有一个双向的 OneToMany 关联:一个账户有多个订单,一个订单有一个账户。其中一种关联与另一种关联相反。因此,您必须在一侧(通常是多侧:这是 JPA 强制执行的)定义此关联的映射,并声明该关联与另一侧(一侧)的另一个关联相反:

@Entity
public class Account implements Serializable, IEntity{
    // ...
    private List<Order> orders; 

    // mappedBy is used to indicate that this association is the inverse of the association
    // mapped by the account property of the Order entity
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="account")
    public List<Order> getOrders() {
        return orders;
    }       
}

@Entity
public class Order implements Serializable, IEntity {
    // ...
    // a relationship to another entity is never mapped by @Column. You can't have annotations 
    // on the field and the getter. Choose one or the other, and be consistent for all 
    // the properties of the entity
    private Account account; 

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ACCOUNT_ACCOUNTID")
    public Account getAccount() {
        return account;
    }  
}

请注意,大多数目前,在关系的多方上进行级联是错误的。您不希望订单下达后帐户被删除,是吗?账户持有订单,而不是相反。

最后,关于您的查询:它不是 SQL 查询,而是 HQL 查询。而且你不应该使用连接,而应该使用绑定参数:这更安全(无注入攻击),更清晰(无需引用,更具可读性),并且更快:

String hql = "from Account a where a.balance = :theBalance";
Query query = session.createQuery();
query.setParameter("theBalance", new BigDecimal("12.5");

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) :

@Entity
public class Account implements Serializable, IEntity{
    // ...
    private List<Order> orders; 

    // mappedBy is used to indicate that this association is the inverse of the association
    // mapped by the account property of the Order entity
    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy="account")
    public List<Order> getOrders() {
        return orders;
    }       
}

@Entity
public class Order implements Serializable, IEntity {
    // ...
    // a relationship to another entity is never mapped by @Column. You can't have annotations 
    // on the field and the getter. Choose one or the other, and be consistent for all 
    // the properties of the entity
    private Account account; 

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ACCOUNT_ACCOUNTID")
    public Account getAccount() {
        return account;
    }  
}

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 :

String hql = "from Account a where a.balance = :theBalance";
Query query = session.createQuery();
query.setParameter("theBalance", new BigDecimal("12.5");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文