2017-07-28 20 views
-1

私はScala + Hibernate(1つのバンクには多くのブランチがあります)で一対多の関係を実装しようとしています。私は次のエラーを取得する:Scala + Hibernateで1対多の関係を実装する

Initial SessionFactory creation failed.org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: admin.bank.manage.BankHib.branches

これらのテーブルです:

CREATE TABLE banks (
    sk int NOT NULL, 
    code varchar(10) DEFAULT NULL, 
    name varchar(100) DEFAULT NULL, 
    version smallint DEFAULT NULL, 
    PRIMARY KEY (sk) 
) 

CREATE TABLE branches (
    sk smallint NOT NULL, 
    code varchar(10) DEFAULT NULL, 
    name varchar(100) NOT NULL, 
    descrip varchar(200) DEFAULT NULL, 
    bank_sk tinyint NOT NULL, // <== this is the bank sk column 
    notes text, 
    version smallint DEFAULT NULL, 
    state char(2) NOT NULL, 
    city varchar(45) DEFAULT NULL, 
    county varchar(45) DEFAULT NULL, 
    county_id int DEFAULT NULL, 
    zipcode varchar(20) DEFAULT NULL, 
    PRIMARY KEY (sk) 
) 

そして、これらは、Hibernateのクラスです:

@Entity 
@Table(name = "banks") 
class BankHib { 

    @Id 
    var sk: Int = _ 

    var code: String = _ 
    var name: String = _ 
    var version: Int = _ 

    @OneToMany(orphanRemoval=false) 
    @JoinColumn(name="bank_sk") 
    var branches: Seq[BranchHib] = _ 
} 


@Entity 
@Table(name = "branches") 
class BranchHib { 

    @Id 
    var sk: Int = _ 

    var code: String = _ 
    var name: String = _ 
    var descrip: String = _ 
    var city: String = _ 
    var county: String = _ 

    @Column(name = "county_id") 
    var countyId: Int = _ 
    var state: String = _ 
    var zipCode: String = _ 
    var notes: String = _ 

    @ManyToOne 
    @JoinColumn(name="bank_sk", nullable=false) 
    var bank: BankHib = _ 

    var version: Int = _ 
} 

私はその枝の選択を実行すると、エラーが発生しました関連する銀行も検索する必要があります。

val list = session.createQuery("from BranchHib order by name").list. 
          asScala.toList.map(_.asInstanceOf[BranchHib]) 

この作品を作成するには?

答えて

1

休止user guide状態:

Hibernate uses its own collection implementations which are enriched with lazy-loading, caching or state change detection semantics. For this reason, persistent collections must be declared as an interface type. The actual interface might be java.util.Collection , java.util.List , java.util.Set , java.util.Map , java.util.SortedSet , java.util.SortedMap or even other object types (meaning you will have to write an implementation of org.hibernate.usertype.UserCollectionType).

@OneToMany(orphanRemoval=false) 
@JoinColumn(name="bank_sk") 
var branches: Seq[BranchHib] = _ 

あなたはSeqするbranchesを定義しますが、HibernateはScalaのコレクション型を認識しません。 branchesのタイプをjava.util.List[BranchHib]に変更します。代わりに、配列の

0

利用のjava.util.List:

@OneToMany 
@JoinColumn(name="bank_sk") 
var branches: java.util.List[BranchHib] = _