• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

EJB3 Beta Certification - EntityManager BASIC notes. (PART II)

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
continuance...

-JPA requires only two pieces of metadata: @javax.persistence.Entity and @javax.persistence.Id.
-by default, all properties are persistent.
-Instead of O/R annotations you can, alternatively, use the orm.xml (WEB-INF/) to describe the mapping.

<entity-mappings>
<entity class="mypackage.MyClass" access="PROPERTY">
<attributes>
<id name="id">
</attributes>
</entity>
</entity-mappings>


===elementary schema mappings===
@Entity
@Table(name="TABLE_NAME")
@Id
@Column(name="COL_NAME", nullable=false, columnDefinition="integer")
@Column(name="COL_NAME", length=20)

-Every entity bean must have a PK (and it must be unique, dah).
-You can generate the PK manually or using the @javax.persistence.GeneratedValue annotation.

Is possible use tables to get the generated IDs.
@TableGenerator(name="CUST_GENERATOR" ...)
@Id
@GeneratedValue(strategy=GenerationType.TABLE, generator="CUST_GENERATOR")

Using Oracle Sequence Generators...
@SequenceGenerator(name="CUSTOMER_SEQUENCE", sequenceName="CUST_SEQ")
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CUSTOMER_SEQUENCE")


===Composite Keys===
There are two ways to map composite keys: @javax.persistence.IdClass and @javax.persistence.EmbeddedId.
NOTE: PK autogeneration is not supported for composite keys.

-The first way to define a composite PK is using @IdClass
You need to create a PK composite class and this class must:
.be serializable
.have a public no-arg constructor
.implements equals() and hashCode()

@Entity
@IdClass(MyPKComposite.class) //a serializable, +equals() +hashCode, no-public constructor, class.
public class SomeClass implements java.io.serializable {
//NOTE The SomeClass must have the same exact properties as the MyPKComposite class
private String firstName;
private String lastName;
private long ssn;

private String getFirstName() { return... }

@Id
private String getLastName() { return... }
@Id
private long getSsn() { return... }

//... and setters.
}

Another whay to define a PK composite is using the @EmbeddedId annotation:
-On this scenario, you need to create a PK composite class with the @Embeddable annotation
-You also can use @Embedded to embed nonentity Java Objetis (do not have any identity) within your entity bean.

@Embeddable
public class MyPKComposite implements java.io.serializable {
//... column mappings
@Column(name="REAL_COLUMN_NAME") //if you dont like this, you can use @AttributeOverrides instead.
public String getLastName() { //... }
}

@Entity
public class SomeClass implements java.io.serializable {
private String firstName;
private MyPKComposite pk;

@EmbeddedId
public MyPKComposite getPk() { //... }

// --- OR (dependes if @Column was not used in PK definition class) ----

@@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="lastName", column=@Column(name="REAL_COLUMN_NAME")),
//... all overrides
})
public MyPKComposite getPk() { //... }

//...other getters and setters.
}


===== property mappings =====

-@Transient: By default, all properties are persistent - use @Transient to avoid this behaviour.
-@Basic(fetch=FetchType.LAZY, optional=false) (usually you would never need this)
.Is just a hint! the provider could use FetchType.EAGER even if you choose LAZY.
.when optional is true, the property is treated as nullable.
-@Temporal provides aditional information about date/time properties in DB.
@Temporal(TemporalType.DATE)
@Temporal(TemporalType.TIME)
@Temporal(TemporalType.TIMESTAMP)
-@Lob to persist in a Blob or Clob DB field.
@Lob
@Basic(fetch=FetchType.Lazy, optional=true)
public JPEG getPicture() { //... };
-@Enumerated maps Java enum types to the DB.
@Enumerated(EnumType.STRING) //String enum value instead of ORDINAL (default) number.
publc MyEnum getMyEnum() { //... }
-@SecondaryTable to deal with one logical entry stored in two (OR MORE) different tables.
eg. A CUSTOMER_TABLE and an ADDRESS_TABLE
@Entity
@Table(name="CUSTOMER_TABLE", pkJoinColumns={@PrimaryKeyJoinColumn(name="ADDRESS_ID")})
public class Customer implements java.io.serializable {
//...
@Column(name="STREET", table="ADDRESS_TABLE")
//...
 
Bartender
Posts: 3955
43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wanderlei Souza:


@Entity
@IdClass(MyPKComposite.class) //a serializable, +equals() +hashCode, no-public constructor, class.
public class SomeClass implements java.io.serializable {
//NOTE The SomeClass must have the same exact properties as the MyPKComposite class




no-public constructor



must be:


no-args public constructor



Good job !

MZ
 
Blood pressure normal? What do I change to get "magnificent"? Maybe this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic