How good practice and how is Transient Objects in POJO / Models in Hibernate Objects through "Program To Interface"?

StackOverflow https://stackoverflow.com/questions/22091015

문제

I am working on basic financial/banking projects. There is "Money" with different contexts, not only currency type texts but also I need other manipulations.

In DB, I persist the amount of money as NUMBER field, and in Hibernate as BigDecimal type object. Let's say Transfer is an object mapped to a table which has the information of money transfer between two people;

@Entity
class Transfer {
    @Id @Column
    private Long id;

    @Column
    private Person from; //mapped personId

    @Column
    private Person to; //mapped personId

    @Column
    private BigDecimal amount;

    //... setters & getters 
}

But actually; almost always I need the amount as Money and with special toString() method: asText() , not only the BigDecimal object and because I need this exactly:

Transfer t = dao.getMySpecialTransferObject();
System.out.println(t.money.asText());

But I still need the amount directly, because I need to do some calculations on the amount of money. (And I need Money type objects in other Entities as well)

Now I guess I need to create such a class;

class Money {
    private BigDecimal amount;
    //... setters & getters

    public String asText(){
        String textToReturn = "The amount of this money is: " + amount.toString;
        // ... and some locale information and manipulation on textToReturn
        return textToReturn;
    }
}

As complexicity grows, Design Patterns say "program to interface", then I think I will do this Money as interface and an implementation;

interface Money{
    public String asText();
}

class BasicMoney implement Money{
    private BigDecimal amount;
    //... setters & getters

    public String asText(){
        String textToReturn = "The amount of this money is: " + amount.toString();
        // ... and some locale information and manipulation on textToReturn
        return textToReturn;
}

So I want to edit my POJO "Transfer" with "Money" type object not as amount.

@Entity
class Transfer {
    @Id @Column
    private Long id;

    @Column
    private Person from; //mapped personId

    @Column
    private Person to; //mapped personId

    @Column
    // Not anymore: private BigDecimal amount;
    private Money money;

    //... setters & getters 
}

Alright, now here is the question:

My POJO "Transfer" would have same "Money" type object but what about DB relations? Money is not a persistent object in DB, so I might use @Transient annotation but I still need a coupling/relation between Money and the "amount" column in DB. How should I keep this relation on POJO?

Note; We are doing this formatting etc. with util classes (without interfaces or transient objects) but I am not sure about this if this is the best practice and I don't want to develop util classes anymore unless there is no better solution.

도움이 되었습니까?

해결책

Keep your field as BigDecimal and just add helper class with static function which formats/displays your amount of money. In this case extracting Money interface doesn't make sense, because you have only "toString" method. Keep in mind the KISS principle.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top