Related: Trouble Shooting

Constructors

We don't need a default constructor and this means we can give our entity beans a constructor that takes arguments. Typically the arguments are properties that should always be supplied when creating a new bean of that type.

e.g. Customer must have a name

...

@Entity
public class Customer {

  @Id
  long id;

  String name;

  ... // other properties

  /**
   * To create a new Customer we need to supply a name.
   */
  public Customer(String name) {
    this.name = name;
  }

  // getters and setters

}

e.g. Order must have a Customer and order date

...

@Entity
public class Order {

  @Id
  long id;

  @Column(nullable=false)
  LocalDate orderDate;

  @ManyToOne(optional=false)
  Customer customer;

  ... // other properties

  public Order(LocalDate orderDate, Customer customer) {
    this.orderDate = orderDate;
    this.customer = customer;
  }

  // getters and setters

}

Edit Page