我是Hibernate的新手。我在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 )
)
帐户表:
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;
}
}
订单类:
@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+"'";
它给了我以下的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)
请帮帮我。在此先感谢。
答案 0 :(得分:2)
您在这里有一个双向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");