我使用DAO MVC,经过一些谷歌搜索,我考虑将一些变量存储为 Enum in java 和 String in MySQL 。所以我在Item.java中创建(将持久存入Item表)静态初始化和静态方法将Enum转换为String,反之亦然。 但是有人说我在静态初始化和静态方法之后我的Item.java类变成了NOT POJO。
问题:
已编辑:我的代码:
package model;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
public class Order {
public enum OrderStatus {
NOT_REVIEWED,
APPROVED,
REJECTED,
RETURNED
}
// ==================
// = Transient =
// ==================
private static final Map<String, OrderStatus> convertStringToOrderStatusMap = new HashMap<String, OrderStatus>(3);
private static final Map<OrderStatus, String> convertOrderStatusToStringMap = new EnumMap<OrderStatus, String>(OrderStatus.class);
static {
convertStringToOrderStatusMap.put("not reviewed", OrderStatus.NOT_REVIEWED);
convertStringToOrderStatusMap.put("approved", OrderStatus.APPROVED);
convertStringToOrderStatusMap.put("rejected", OrderStatus.REJECTED);
convertStringToOrderStatusMap.put("returned", OrderStatus.RETURNED);
convertOrderStatusToStringMap.put(OrderStatus.NOT_REVIEWED, "not reviewed");
convertOrderStatusToStringMap.put(OrderStatus.APPROVED, "approved");
convertOrderStatusToStringMap.put(OrderStatus.REJECTED, "rejected");
convertOrderStatusToStringMap.put(OrderStatus.RETURNED, "returned");
}
// ==================
// = Attributes =
// ==================
private Integer orderId; //Primary key
private OrderStatus status;
private Integer reimbursement;
private String firstName;
private String secondName;
private String passportData;
private String pickUpDate;
private String dropOffDate;
//java.util.Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-05-18 16:29:31");
private String customerCell;
private String customerAddress;
// ==================
// = Foreign Keys =
// ==================
private User user;
private Car car;
// ==================
// = Public methods =
// ==================
public Integer getOrderId() {
return orderId;
}
public void setOrderId(Integer orderId) {
this.orderId = orderId;
}
public String getStatus() {
return convertOrderStatusToString(status);
}
public void setStatus(OrderStatus status) {
this.status = status;
}
public Integer getReimbursement() {
return this.reimbursement;
}
public void setReimbursement(Integer value) {
this.reimbursement = value;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public String getPassportData() {
return passportData;
}
public void setPassportData(String passportData) {
this.passportData = passportData;
}
public String getPickUpDate() {
return pickUpDate;
}
public void setPickUpDate(String pickUpDate) {
this.pickUpDate = pickUpDate;
}
public String getDropOffDate() {
return dropOffDate;
}
public void setDropOffDate(String dropOffDate) {
this.dropOffDate = dropOffDate;
}
public String getCustomerCell() {
return customerCell;
}
public void setCustomerCell(String customerCell) {
this.customerCell = customerCell;
}
public String getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(String customerAddress) {
this.customerAddress = customerAddress;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public static OrderStatus converStringToOrderStatus(String status) {
return convertStringToOrderStatusMap.get(status);
}
public static String convertOrderStatusToString(OrderStatus status) {
return convertOrderStatusToStringMap.get(status);
}
}
答案 0 :(得分:0)
因为 P lain O ld J ava O 对象只有数据。添加逻辑和方法意味着它不再是Plain Old Java Object。
这并不一定会使它成为一件坏事,但你可能能够将逻辑重构为它自己的类。
答案 1 :(得分:0)
让我们忽略POJO。
他们的意思是面向服务与域驱动。
面向服务遵循严格的行为与国家分离。他们调用POJO数据对象,这些数据对象基本上是美化结构。因此,您可以将静态方法放在服务中。事实上,您可能甚至不希望方法是静态的,因为这也违反了面向服务的方法(参见依赖注入和邪恶的单例)。
Domain Driven遵循经典OOP(例如Rails Active Record)的想法,他们认为可以将行为放入他们的POJO中。因此,因为状态+行为是耦合的,所以只有一个实现,因此域对象中的静态方法都可以。
如果你走DAO路线,你最有可能做面向服务。我的意见是,如果您要进行DAO POJO路线,则应使用immutable objects (shameless plug)来表示这些数据对象。
最后根据我的知识将内联enum
放入一个类中并不违反POJO的任何定义。这就是说,自从你使用JPA以来,你应该知道@Enumerated
。