ドキュメンテーション / マッピング / コンストラクタ
既定のコンストラクタ
エンティティBeanに既定のコンストラクタがまだ存在しない場合は、Ebeanエンハンスメントによって自動的に既定のコンストラクタが追加
されます。
例 - デフォルトのコンストラクタのない顧客
下の顧客エンティティ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のフィールドを作成できます。顧客Bean(nameプロパティなし)の部分的な読み込みや、参照Bean(@Idプロパティのみが読み込まれる)の作成にEbeanによる制限はありません。
// 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();
例 - セッターなしの国
下の国エンティティBeanにはプロパティが2つのみあり、どちらもコンストラクタで設定されます。この場合、このエンティティBeanにはセッターがありません。
/**
* 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で記述された国
codeとnameのプロパティは非null/必須(型の宣言後に?がありません)です。
@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で顧客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で記述された顧客
顧客名は常に非nullとして宣言され(nameの型の宣言後に?はありません)、新しい顧客インスタンスを構成する際に渡す必要があります。
@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()