2012-02-10 16 views
1

私のWebアプリケーションにはmySQLでplayframworkを使用しています。私は複数の列にunique_constraintが必要なテーブルを持っています。Play Framework [1.2.4]:エンティティの一意の制約

私は次のように定義されたエンティティを持っている...

package models; 

import java.util.Date; 

import javax.persistence.Entity; 
import javax.persistence.JoinColumn; 
import javax.persistence.ManyToOne; 
import javax.persistence.Table; 
import javax.persistence.UniqueConstraint; 

@Entity 
@Table(name = "table", 
        uniqueConstraints = { @UniqueConstraint(columnNames = 
                  { "col_1", "col_2" }) }) 
public class Table extends Model{ 

    @ManyToOne 
    @JoinColumn(name = "col_1") 
     public Column1 col1; 

     @ManyToOne 
    @JoinColumn(name = "col_2") 
     public Column2 col2; 

     @ManyToOne 
    @JoinColumn(name = "col_3") 
     public Column3 col_3; 

} 

列1および列2は 表エンティティと関係を持つ異なるエンティティです。

重複した "col_1とcol_2"の値を として挿入しようとするとエラーは表示されません。データはテーブル (MySQL)に正しく挿入されます。

Table table1 = new Table(); 
table1.col1 = new Column1("1"); 
table1.col2= new Column2("2); 
table1.col3= new Column3("3"); 
table1.save(); 

Table table2 = new Table(); 
table2 .col1 = new Column1("1"); 
table2 .col2= new Column2("2); 
table2 .col3= new Column3("3"); 
table2 .save(); 

私はテーブルにunique_constraintを手動で作成する必要がありますか?

、私は理解して助けてください、私は上記の 実装に欠けている何かを持っている場合、テーブルはJPAによって作成されていない場合

おかげで、 カルティク

答えて

2

は、はい、あなたは手動で作成する必要がありますそれらが影響を及ぼすためのユニークな制約。

表があらかじめ存在しない場合、JPAは制約を作成する必要があります。

JPAは、既存のテーブルに対して制約を作成しません。

関連する問題