Introduction
embedded objects used in the event of conflict the object model and physical model database. For example, the user is being linked to being address. The object model of these entities will be implemented by separate objects, but in a relational database in order to achieve better performance can be implemented using a single table, containing data from both user and his address. In this example, the URL will be built and will be subject to belong to the permanent facility user. An important feature is the built-in objects that are not sustained its identity (its identifier) \u200b\u200band can not be shared by different objects and equipment. Built Classembedded object class must be marked with a note
Embeddable
. This does not mean it as an entity, the more determined not to her table in the database, or ID. In addition to wbudowywanej this class to be specified as the field mapping for simple entities. Below is an example of a class definition wbudowywanej - Address: pl.dwalczak.jpapg2.model package imports, javax.persistence.Column imports, javax.persistence.Embeddable; @ Embeddable public class Address {private String city; private String postcode; private String street; private String number; @ Column (length = 50, Nullable = false) public String getCity () {return city;} public void setCity (String city) {this.city = city;} @ Column (length = 6, Nullable = false) public String getPostcode () {return postcode;} public void setPostcode (String postcode) {this.postcode = postcode;} @ Column (length = 50, Nullable = false) public String getStreet () {return street;} public void setStreet (String street) {this.street = street;} @ Column (length = 10, Nullable = false) public String getNumber () {return number;} public void setNumber (String number) {this.number = number;}}
wbudowywanych Placing objects on the premises and equipment
Class fields of the type designated as a bear in a built Embedded in a note
. But it is not necessary, since the specification requires that the JPA EntityManager to seek out the same matter on the basis of annotations Embeddable
, which bears the class of the field. The fields are mapped to an object embedded in the columns of the table entity to which it belongs. Overriding attributes JPA mapping allows to overwrite the attributes defined in the mapping class wbudowywanej. For this purpose the annotation
AttributeOverrides
, which contains a list of endorsements AttributeOverride
. And these only serve to override the mapping of each field class. Mechanism to override the mapping of attributes are big opportunities in the context of reusable class definitions wbudowywancyh. Below is an example of an entity, which includes two buildings built on the type of address (permanent residence address and mailing address.) pl.dwalczak.jpapg2.model package imports, javax.persistence.AttributeOverride; javax.persistence.AttributeOverrides import, import javax.persistence.Column imports, javax.persistence.Embedded; javax.persistence.Entity import, import javax.persistence. GeneratedValue; javax.persistence.GenerationType import, import javax.persistence.Id; javax.persistence.NamedQuery import, import javax.persistence.SequenceGenerator imports, javax.persistence.Table; @ NamedQuery (name = "user.findByNickName", query = "select u from User u WHERE u.nickName =: nickname") / / Define a sequence of "users_seq" used to generate the primary key table "users". @ SequenceGenerator (name = "users_seq") / / mapping table "users". @ Entity @ Table (name = "users") public class User {private Long id; private String nickname; private Address address; private Address mailingAddress; @ Id @ GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "users_seq") @ Column (name = "usr_id") public Long getId () {return id;} protected void setId (Long id) {this.id = id;} @ Column (name = "usr_nickname", unique = true, Nullable = false, length = 24) public String getNickName () {
return nickName;
}
public void setNickName(String name) {
this.nickName = name;
}
@Embedded
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Embedded
@AttributeOverrides({
@AttributeOverride(name="city", column=@Column(name="ma_city")),
@AttributeOverride(name="postcode", column=@Column(name="ma_postcode")),
@AttributeOverride(name="street", column=@Column(name="ma_street")),
@AttributeOverride(name="number", column=@Column(name="ma_number"))
})
public Address getMailingAddress() MailingAddress {return;} public void setMailingAddress (Address mailingAddress) {this.mailingAddress = mailingAddress;}}
Resources
example source filesHibernate Annotations - 2.2.2.3. Embedded objects (aka components)
JPA specification
0 comments:
Post a Comment