我正在尝试使用Hibernate处理OneTo诸多关系。我正在使用@Temporal注释告诉hibernate有关Data字段的信息。我不确定为什么我在这里得到这个错误。看来日期格式有问题。请让我知道如何解决它。
顾客
package regular;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
public class Customers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int customer_id;
private String customerName;
private String contactName;
private String address;
private String city;
private String postalCode;
private String country;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
@JoinColumn(name = "customer_id")
private List<Orders> order;
public Customers() {
}
public List<Orders> getOrder() {
return order;
}
public void setOrder(List<Orders> order) {
this.order = order;
}
public Customers(String customerName, String contactName, String address, String city, String postalCode,
String country, List<Orders> order) {
this.customerName = customerName;
this.contactName = contactName;
this.address = address;
this.city = city;
this.postalCode = postalCode;
this.country = country;
this.order = order;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public String getContactName() {
return contactName;
}
public void setContactName(String contactName) {
this.contactName = contactName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPostalCode() {
return postalCode;
}
public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public int getCustomer_id() {
return customer_id;
}
@Override
public String toString() {
return "Customers [customer_id=" + customer_id + ", customerName=" + customerName + ", contactName="
+ contactName + ", address=" + address + ", city=" + city + ", postalCode=" + postalCode + ", country="
+ country + ", order=" + order + "]";
}
}
订单
package regular;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int orderId;
@Temporal(value = TemporalType.TIMESTAMP)
private Date orderDate;
private String productName;
private int quantity;
public Orders(String productName, int quantity) {
this.orderDate = new Date();
this.productName = productName;
this.quantity = quantity;
}
public Orders() {
}
public Date getOrderDate() {
return orderDate;
}
public void setOrderDate(Date orderDate) {
this.orderDate = orderDate;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getOrderId() {
return orderId;
}
@Override
public String toString() {
return "Orders [orderId=" + orderId + ", orderDate=" + orderDate + ", productName=" + productName
+ ", quantity=" + quantity + "]";
}
}
跑步者
package regular;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Runner {
public static void main(String[] args) {
SessionFactory sessionFactory = new Configuration().configure("/regular/hibernate.cfg.xml")
.addAnnotatedClass(Customers.class).addAnnotatedClass(Orders.class).buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
Customers customer = new Customers();
customer.setCustomerName("Robert Bosch");
customer.setAddress("404 California Ave");
customer.setCity("California");
customer.setPostalCode("60466");
customer.setCountry("USA");
List<Orders> orders = new ArrayList<>();
orders.add(new Orders("Car", 4));
orders.add(new Orders("Headphones", 6));
customer.setOrder(orders);
session.save(customer);
session.close();
}
}
错误
Caused by: java.sql.SQLSyntaxErrorException: Unknown column 'orderDate' in 'field list'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:686)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:663)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:653)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2041)
at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1827)
at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2041)
at com.mysql.cj.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:1977)
at com.mysql.cj.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:4963)
at com.mysql.cj.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1962)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:204)
... 41 more
以下链接可能对某些用户有所帮助:
Spring Boot的Hibernate字段命名问题(命名策略)
在应用程序中添加以下内容。属性文件:
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
默认情况下,hibernate将驼峰大小写字母转换为下划线。因此,您要么更改表中的列以反映这一点,要么更改hibernate命名策略。
我的表名和数据库中似乎有问题。我已经更改了表名,并且成功了。谢谢大家!
所以首先我为我不可靠的解释道歉。我的PHP技能需要认真改进。 错误消息表示“”未知。我知道这不是真的,因为当我创建一个名为“”的变量并回显前者时,我会得到编号。 以下是完整的错误消息: 致命错误:未捕获异常“PDOException”,消息为“SQLSTATE[42S22]:未找到列:C:\xampp\htdocs\loginregister master\addnew中的“字段列表”中的1054
我需要帮助。 使用方法时,在HQL中出现错误: 我猜他必须写而不是 可能是我做错了实体和关系吗? 2个实体-房屋和街道 ER模型: 餐桌街道 ID 名称 houses_id 桌房 ID 名称 我的类: 街道 房屋 我的道小鬼: StreetDaoImp: HouseDaoImpl: 错误:
即使我在表中设置了关系和定义了外键,我得到这个错误的原因是什么?
问题内容: 我需要帮助。 当我使用方法时,我在HQL中有错误: 我猜他一定要写代替 可能是我做错了实体和关系吗? 2个实体-房屋和街道 ER-model: Table Streets ID Name Houses_id Table Houses ID name My Classes: Street House My DAOIMP: StreetDAOImp: HouseDAOImpl: 错误: 问
问题内容: 我试图将值添加到phpmyadmin中的表中,但出现错误:“字段列表”中的未知列“ …”。 这是我的代码: 因此,当我在上一页的表单中输入fds作为名称时,我得到:“字段列表”中的未知列“ fds”。这以前从未发生过,我也不知道发生了什么。 问题答案: 我认为这 应该
我正在编写一个spring boot+Hibernate JPA应用程序,我的Hibernate代码不工作,下面是详细信息 实体类: 服务/DAO: 我已经检查了实体类中的表名和列名,所有这些都是正确的,这是一个简单的映射,我也不涉及第二个表。