0

I use DAO MVC, and I after some googling I consider to store some variables as Enum in java and String in MySQL. So I create in Item.java (that will be persist into Item table) static initialization and static methods to convert Enum into String and vise versa. But someone said me that after this static initialization and static methods my Item.java class became NOT POJO.

Question:

  1. Why it became NOT POJO?
  2. And if I'll make those methods not static Item.java class will be POJO?

EDITED: MY code:

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);
    }
}
VB_
  • 45,112
  • 42
  • 145
  • 293
  • Paste your code and I'll tell you if your doing something bad. – Adam Gent Apr 09 '13 at 19:27
  • And Am I doing something bad? – VB_ Apr 10 '13 at 14:34
  • Well not bad but could be better. I would use [`EnumMap`](http://docs.oracle.com/javase/6/docs/api/java/util/EnumMap.html) for the `convertOrderStatusToStringMap` and Guava's ImmutableMap.Builder for the other map. If your using the latest Guava than you will have its [ImmutableEnumMap builder you can use](http://stackoverflow.com/a/11244901/318174). – Adam Gent Apr 10 '13 at 15:59

2 Answers2

0

Because a Plain Old Java Object only has data. Adding logic and methods means that it's no longer Plain Old Java Object.

That doesn't necessarily make it a bad thing, but you might be able to refactor the logic out into a class of it's own.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • That's not my understanding of what POJO means (see http://en.wikipedia.org/wiki/Plain_Old_Java_Object). A POJO can have methods. What makes it a POJO is whether it's dependent on external classes (e.g. inheriting from a framework class or relying on an annotation). – Jacob Mattison Apr 09 '13 at 19:00
  • 1
    Actually the definition of POJO is somewhat nebulous. – Adam Gent Apr 09 '13 at 19:00
  • It really depends if he is doing SOA or DDD. For example Spring Roo makes enhanced AspectJ @Configurable POJO's that they call POJOs that follow the Active Record pattern. – Adam Gent Apr 09 '13 at 19:02
  • "Because a Plain Old Java Object only has data"... I think the "only has data" is a better definition for a Java Bean, which is based in setter and getter for the properties and doesn't have logic inside it. – fmodos Apr 09 '13 at 19:03
  • My Item.class (that must be POJO) has inner enum-class and use this class in static methods. What I should do to make it POJO? – VB_ Apr 09 '13 at 19:18
  • @AndrewMelnuk I wouldn't worry about it. Chances are your using JPA which IMHO violates POJO (it enhances the object). I make inner enum classes and static factory methods all the time. Just make sure your methods are not doing something dumb like connecting to the database. Also although Enum's require static initialization so do class declarations :) – Adam Gent Apr 09 '13 at 19:21
0

Lets ignore POJO.

What they mean is Service Oriented vs Domain Driven.

Service Oriented follows strict separation of behavior from state. They call POJOs data objects which are essentially glorified structs. Thus you would put the static methods in the Service. In fact you probably wouldn't even want the methods static as that is also against the service oriented approach (see dependency injection and evil singleton).

Domain Driven follows the idea of classic OOP (e.g. Rails Active Record) in which they do believe its OK to put behavior in their POJOs. Consequently because state + behavior are coupled there is only one implementation and thus static methods in the domain object are OK.

If your going the DAO route your most likely doing Service Oriented. My opinion is if your going to do the DAO POJO route you should use immutable objects (shameless plug) for those data objects.

Finally putting an inline enum into a class from my knowledge does not violate any definition of POJO. That being said you should know about @Enumerated since your using JPA.

Adam Gent
  • 47,843
  • 23
  • 153
  • 203