默认构造函数

如果实体 Bean 上还没有默认构造函数,Ebean 增强将自动添加一个默认构造函数

示例 - Customer,无默认构造函数

在下面的示例 Customer 实体 Bean 中,没有默认构造函数,Ebean 的增强将自动添加一个。

/**
 * Customer entity bean with no default constructor.
 */
@Entity
public class Customer {

  public static final CustomerFinder find = new CustomerFinder();

  @NotNull @Size(max = 100)
  String name;

  ...

  // enhancement will add a default constructor
  // if there is not one on the entity bean

  public Customer(String name) {
    this.name = name;
  }

  ... // getters, setters etc

部分对象

请注意,如果你愿意,可以将字段设为 final,Ebean 对于部分加载 customer Bean(没有 name 属性)或创建引用 Bean(仅加载其 @Id 属性)没有任何限制。

// fetch partially populated beans that
// don't have the name property loaded

List<Customer> customers =
  Customer.find.where()
    .name.startsWith("Rob")
    .select("id") // only select id property
    .findList();

引用 Bean

Ebean 可以构造仅加载其@Id属性的引用 Bean。

// reference bean with only @Id property loaded
Customer refBean = Customer.find.ref(42);

// reference beans don't hit the database unless
// lazy loading is invoked

// invoke lazy loading as name property is not loaded
String name = refBean.getName();

示例 - Country,无 setter

在下面的 Country 实体 Bean 中,只有 2 个属性,并且这两个属性都在构造函数中设置。在这种情况下,此实体 Bean 上没有 setter。

/**
 * Country entity bean with no default constructor and
 * no setters (only getters).
 */
 @Entity
 public class Country extends Model {

   public static final CountryFinder find = new CountryFinder();

   @Id @Size(max = 2)
   final String code;

   @NotNull @Size(max = 60)
   final String name;

   // enhancement will add a default constructor

   public Country(String code, String name) {
     this.code = code;
     this.name = name;
   }

   // getters only for Country bean

   public String getCode() { return code; }

   public String getName() { return name; }

 }

Ebean 仍然可以支持部分加载的实体 Bean 和引用 Bean。

// insert a country
new Country("NZ", "New Zealand").save();

// reference bean (only has @Id property loaded)
Country nzRefBean = database.getReference(Country.class, "NZ");

// reference bean using a "finder"
Country nzRefBean = Country.find.ref("NZ");

Kotlin

用 Kotlin 编写的 Country

code 和 name 属性是非空/必需的(类型声明后没有 ?)。

@Entity
@Table(name = "country")
class Country (

    @Id @Size(max = 2)
    var code: String, // non-null type

    @NotNull @Size(max = 60)
    var name: String  // non-null type

) : Model() {

  override fun toString(): String {
    return "code:$code name:$name";
  }

  companion object : CountryFinder() {}
}

... 在 Kotlin 中使用 Customer Bean。

Country("NZ", "New Zealand").save()

// reference bean
val nzRef = Country.ref("NZ")

// finder & query bean use
val nz = Country.where()
    .code.equalTo("NZ")
    .findOne()

用 Kotlin 编写的 Customer

customer name 属性声明为始终非空(name 的类型声明后没有 ?),并且在构造新的 customer 实例时必须传递。

@Entity
@Table(name = "customer")
class Customer(

  @NotNull @Size(max = 100)
  var name: String,  // non-null type

  @SoftDelete
  var deleted: Boolean = false,

  var registered: Date? = null,

  @Size(max = 1000)
  var comments: String? = null,

  @ManyToOne(cascade = arrayOf(CascadeType.ALL))
  var billingAddress: Address? = null,

  @ManyToOne(cascade = arrayOf(CascadeType.ALL))
  var shippingAddress: Address? = null,

  @OneToMany(mappedBy = "customer", cascade = arrayOf(CascadeType.PERSIST))
  var contacts: MutableList<Contact> = ArrayList()

) : BaseModel() {

  companion object find : CustomerFinder() {}

  override fun toString(): String {
    return "customer(id:$id name:$name)";
  }
}

... 在 Kotlin 中使用 Customer。

// new customer (requires name)
val rob = Customer("Rob")
rob.save()

// reference bean
val refBean = Customer.ref(42L)

// finder / query bean use
val customers =
  Customer.find.where()
    .name.istartsWith("rob")
    .findList()