我是Java / Spring / Hibernate的新手,我正在为假设的快递服务构建一个小型测试项目。
我现在正在尝试使用javax.persistance / hibernate实现数据库,但是在尝试运行项目时出现错误:
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: courier.order.OrderState, at table: order, for columns: [org.hibernate.mapping.Column(accepted_state)]
这是我的Order
类的代码。它通过OrderState
-接口实现状态设计模式。
package courier.order;
import courier.states.*;
import javax.persistence.*;
@Entity
public class Order implements OrderState{
@Id
@GeneratedValue
public Long id;
@Column(nullable = false)
public String deliverer;
@Column(nullable = false)
public String customer;
@Column(nullable = false)
public String customerStreet;
@Column(nullable = false)
public String customerNumber;
@Column(nullable = false)
public String customerZip;
@Column(nullable = false)
public String deliveree;
@Column(nullable = false)
public String deliverStreet;
@Column(nullable = false)
public String deliverNumber;
@Column(nullable = false)
public String deliverZip;
//attribute-setters
public void setDeliverer(String newDeliverer){
deliverer = newDeliverer;
}
public void setCustomer(String newCustomer){
customer = newCustomer;
}
public void setCustomerStreet(String newCustomerStreet){
customerStreet = newCustomerStreet;
}
public void setCustomerNumber(String newCustomerNumber){
customerNumber = newCustomerNumber;
}
public void setCustomerZip(String newCustomerZip){
customerZip = newCustomerZip;
}
public void setDeliveree(String newDeliveree){
deliveree = newDeliveree;
}
public void setDeliverStreet(String newDeliverStreet){
deliverStreet = newDeliverStreet;
}
public void setDeliverNumber(String newDeliverNumber){
deliverNumber = newDeliverNumber;
}
public void setDeliverZip(String newDeliverZip){
deliverZip = newDeliverZip;
}
//attribute-getter
public String getDeliverer(){
return deliverer;
}
public String getCustomer(){
return customer;
}
public String getCustomerStreet(){
return customerStreet;
}
public String getCustomerNumber(){
return customerNumber;
}
public String getCustomerZip(){
return customerZip;
}
public String getDeliveree(){
return deliveree;
}
public String getDeliverStreet(){
return deliverStreet;
}
public String getDeliverNumber(){
return deliverNumber;
}
public String getDeliverZip(){
return deliverZip;
}
//define states
OrderState newState;
OrderState acceptedState;
OrderState inDeliveryState;
OrderState deliveredState;
OrderState canceledState;
//saved state
OrderState orderState;
//constructor
public Order() {
//initialize states
newState = new NewState(this);
acceptedState = new AcceptedState(this);
inDeliveryState = new InDeliveryState(this);
deliveredState = new DeliveredState(this);
canceledState = new CanceledState(this);
//Initial state
orderState = newState;
}
//State - getter
public OrderState getNewState() { return newState; }
public OrderState getAcceptedState() { return acceptedState; }
public OrderState getInDeliveryState() { return inDeliveryState; }
public OrderState getDeliveredState() { return deliveredState; }
public OrderState getCanceledState() { return canceledState; }
//State - setter
public void setOrderState(OrderState newOrderState) {
orderState = newOrderState;
}
//methods
public void newOrder() {
setOrderState(newState);
}
public String getStatus() {
return orderState.getStatus();
}
public void nextStatus() {
orderState.nextStatus();
}
public void cancelOrder() {
orderState.cancelOrder();
}
}
OrderState接口:
package courier.order;
public interface OrderState {
public String getStatus();
public void nextStatus();
public void cancelOrder();
}
其中一个州,其他州的建造方式相同:
package courier.order.states;
import courier.order.Order;
import courier.order.OrderState;
public class AcceptedState implements OrderState {
Order orderState;
public AcceptedState(Order newState){
orderState = newState;
}
@Override
public String getStatus() {
return "The order was accepted and is being collected by a deliverer.";
}
@Override
public void nextStatus() {
orderState.setOrderState(orderState.getInDeliveryState());
}
@Override
public void cancelOrder() {
}
}
当我注释掉@Entity
注释时,代码可以完美运行