更新! :从HibernateUtil.loadSessionFactory中删除以下行:configuration.addAnnotatedClass(Order.class);
使hibernate能够正确执行。但是订单仍然没有保存到数据库中。
当我使用session.save()
保存对象时,会引发此错误:
`
java.sql.SQLSyntaxErrorException:您的SQL错误 句法;检查与您的MySQL服务器版本相对应的手册 在'Order(orderNumber,totalPrice, 第1行的orderId)值('2019000009',0.5,1)'
`过去可以正常工作,但是在试图使休眠状态与对象的关系进行一点点更改的代码之后,我肯定已经做了一些更改来导致这种情况。
我不是自己写查询的。它是由休眠生成的。 这是我要保存在数据库中的对象:
@Entity
@Table(name = "Order", schema = "products", catalog = "")
public class Order {
@OneToMany
private List<Product> products = new ArrayList<Product>();
@Id
@Column( name = "orderId", nullable = false)
private int orderId;
@Basic
@Column(name = "orderNumber", nullable = false)
private String orderNumber;
@Basic
@Column(name = "totalPrice", nullable = false)
private double totalPrice;
protected Order() {
}
public int getOrderId() {
return this.orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public String getOrderNumber() {
return this.orderNumber;
}
public double getTotalPrice() {
return this.totalPrice;
}
public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
}
public void setTotalPrice(double totalPrice) {
this.totalPrice = totalPrice;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Order that = (Order) o;
return orderId == that.orderId &&
Objects.equals(orderNumber, that.orderNumber) &&
Objects.equals(totalPrice, that.totalPrice);
}
@Override
public int hashCode() {
return Objects.hash(orderId, totalPrice, orderNumber);
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> products) {
this.products = products;
}
}
Hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/products?serverTimezone=UTC</property>
<property name="connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">****</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.pool_size">1</property>
<property name="show_sql">true</property>
<mapping class="nu.educom.calculateChange.Product"/>
<mapping class="nu.educom.calculateChange.Order"/>
<mapping resource="Order.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil:
public class HibernateUtil {
private static SessionFactory sessionFactory = loadSessionFactory();
static {
try{
loadSessionFactory();
}catch(Exception e){
System.err.println("Exception while initializing hibernate util.. ");
e.printStackTrace();
}
}
public static SessionFactory loadSessionFactory(){
Configuration configuration = new Configuration();
configuration.configure("/hibernate.cfg.xml");
configuration.addAnnotatedClass(Order.class);
configuration.addAnnotatedClass(Product.class);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
return configuration.buildSessionFactory(serviceRegistry);
}
public static Session getSession() throws HibernateException {
Session retSession=null;
try {
retSession = sessionFactory.openSession();
}catch(Throwable t){
System.err.println("Exception while getting session.. ");
t.printStackTrace();
}
if(retSession == null) {
System.err.println("session is discovered null");
}
return retSession;
}
public static long insertOrder(Object object) {
loadSessionFactory();
Session session = getSession();
Integer lastInsertId = 1;
Transaction tx = null;
try {
tx = session.beginTransaction();
lastInsertId = (Integer) session.save(object);
tx.commit();
} catch (HibernateException ex) {
if (tx != null) {
tx.rollback();
}
} finally {
session.close();
}
return lastInsertId;
}
}
Order.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name = "nu.educom.calculateChange.Order" table = "order">
<id name = "orderId" type = "int" column="orderId">
<generator class="native"/>
</id>
<property name="orderNumber" column="orderNumber" type="string"/>
<property name="totalPrice" column="totalPrice" type="double"/>
</class>
</hibernate-mapping>
最后,调用保存订单:
public void processOrder() {
Order order = new Order();
order.setTotalPrice(this.getTotal());
order.setOrderNumber("2019000009");
order.setOrderId(1);
for (Product product: this.productsAddedToRegister) {
order.getProducts().add(product);
}
HibernateUtil.loadSessionFactory();
long orderId = HibernateUtil.insertOrder(order);
}
答案 0 :(得分:0)
问题是Order是mysql的函数或关键字。因此,不会执行有关“订单”表的所有查询。将其重命名为“订单”即可解决该问题。