简体   繁体   中英

Is using a domain object as a form-backing object in Spring MVC a bad practice?

Would it be a bad practice to use an instance of Event as a form-backing object in Spring MVC?

One of the problems with this approach comparing to DTOs is that a client can change the hidden fields in a view and that you need to check and reset them. But are there any others?

@Entity
@Table(name = "events")
public class Event
    {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "event_id")
    public int id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    public User userId;

    @Column(name = "amount")
    @NotEmpty(message = "Amount can't be empty")
    public int amount;

    @Column(name = "description")
    @NotEmpty(message = "Description can't be empty")
    public String description;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "entered_by")
    public User enteredBy;

    @Column(name = "entered_at", columnDefinition = "DATETIME")
    @Temporal(TemporalType.TIMESTAMP)
    public Date enteredAt;

    }

The main goal of DTOs is, as you said, isolation. You don't always want the client side to see/access all the fields you have on the DB side.

Moreover, you can use DTOs to aggregate multiple calls from various tables/services and get it back to the client side with one form object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM