Java Persistence 2.0

id:satonaokiさんのひとこと
「地味だね〜、JPA1.1でいいじゃん」
同感。
ふつうに使ってて変わるのはCriteriaの導入、並び順、バリデータくらいかと
id:koichikさんなら「いままでのを0.9にして、次のを1.0にしろ」というかも。

マッピングの拡張

埋め込みオブジェクトの中に埋め込みオブジェクトが入れれる
@Embeddable public class address{
  @Embedded protected zipcode zipcode;
}
@Embeddable public class ZipCode{
  @Length(5) protected String Zip;
  @Length(4) protected string plusfor;
} 
基本的な型や埋め込みクラスのコレクション
@Entity public class Person{
  @Basic protected Set<String> nickNames = new HashSet();
}
@Entity public class WealthPerson extends Person{
  @Embedded protected set<address> vacationHomes = new HashSet();
}
埋め込みオブジェクトの継承
@MappedSuperClass public class Address{
  @Embedded protected ZipCode;
}
@Embeddable public class BusinessAddress extends Address{
}
@Embeddable public class HomeAddress extends Address{
}
並び順の指定
  @OneToMany
  @Ordered @OrderedColumn(name="review_index")
  protected List<Review> reviews = new ArrayList();
フィールドアクセスとプロパティアクセスの混在
@entity public class Customer{
  @Id protected Integer id;

  @AccessType(PROPERTY)
  public Integer getCreditRationg(){
    return rating;
  }
}


外部キーを使った単方向のマッピング
継承の拡張。テーブルごとにクラスを割り当てるTABLE_PER_CLASS

JPQLの拡張

select concat(p.lastname, concat(',',p.firstname)) as n
from person p
order by n

select e.name, e.salary + e.bonus
from employee e
where e.dept.name = 'engineering'

select d.name, sum(c.hourlyrate + c.hoursworked +* 62)
from contractor c join c.dept d
groupby d.name

select e,name
from employee e join e.dept d
where d.name = 'engineering'
and clas(e) in('contractor', 'parttime')

ヒントなどの標準化

EntityManagerFactory、EntityManager、PersistenceContext、クエリーなどのヒントを標準化
JDBCドライバ、ユーザー、パスワード、コネクションプール
キャッシュ、キャッシュサイズ
タイムアウト、ロギング、DDLハンドリング
など

動的クエリー/Criteria

CriteriaQuery cq = em.createCriteria(Customer.class)
 .add(restringions.eq("status", "preferred"))
  .add(restrictions.eq("address.city","newyork"))

Unfetched State

フェッチされてないときの挙動を考え直す

アプリケーション管理のトランザクションがBugの温床

バグ

@Resource EntityManagerFactory emf;
@Resource UserTransaction utx;
void hoge(){
  entitymanager rm = emf.createentitymanager();
  utx.begin();
  ...
  utx.commit();
  em.close();
}

こっちが正しい。(なんでかはよく聴いてない)

@Resource EntityManagerFactory emf;
@Resource UserTransaction utx;
void hoge(){
  utx.begin(); //<-- -- -- ここ -- -- --
  entitymanager rm = emf.createentitymanager();
  ...
  utx.commit();
  em.close();
}

バリデーション

JSR303(Beans Validation)を使ったバリデーション

@Required protected String name;
@Llength(max=5) protected String location code;
@Max(240) protected Integer vacationaccrued;
@AdequatelyCompensated protected Float salary;