2011-12-14 6 views
0

テーブルを作成したいところです。ここでIDは文字列を構成します。コンポジットキーHibernate

 @Entity 
@Table(name = "media_locator") 
public class MediaLocator { 

    private List<MediaObject> mediaObjects; 
    private MediaLocatorPK primaryKey = new MediaLocatorPK(); 

// @Id 
// @GeneratedValue(strategy = GenerationType.AUTO) 
// @Column(name = "id", unique = true, nullable = false) 
// public int getId() { 
//  return id; 
// } 
// 
// public void setId(int id) { 
//   this.id = id; 
// } 

    @EmbeddedId 
    public MediaLocatorPK getPrimaryKey() { 
     return primaryKey; 
    } 

    public void setPrimaryKey(MediaLocatorPK primaryKey) { 
     this.primaryKey = primaryKey; 
    } 



// @Column(name = "location", length = 200) 
    public String getLocation() { 
     return primaryKey.getLocation(); 
    } 


    public void setLocation(String location) { 
     this.primaryKey.setLocation(location); 
    } 

// @Column(name = "description", length = 200, nullable = false) 
    public String getDescription() { 
     return primaryKey.getDescription(); 
    } 

    public void setDescription(String description) { 
     this.primaryKey.setDescription(description); 
    } 


// @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="mediaLocator")  
// public List<MediaObject> getMediaObjects() { 
//  return mediaObjects; 
// } 
// 
// public void setMediaObjects(List<MediaObject> mediaObjects) { 
//   mediaObjects = mediaObjects; 
// } 


    @ManyToMany(
      cascade={CascadeType.ALL}, 
      fetch=FetchType.LAZY) 
    @JoinTable(name = "media_object_location", 
      [email protected](name="mediaLocator_id"), 
      [email protected](name="mediaObject_id")) 
    public List<MediaObject> getMediaObjects() { 
     return mediaObjects; 
    } 

    public void setMediaObjects(List<MediaObject> mediaObjects) { 
     this.mediaObjects = mediaObjects; 
    } 


// @Column(name = "protocol", length = 200, nullable = false) 
    public String getProtocol() { 
     return primaryKey.getProtocol(); 
    } 

    public void setProtocol(String protocol) { 
     this.primaryKey.setProtocol(protocol); 
    } 
// @Column(name = "host", length = 200, nullable = false) 
    public String getHost() { 
     return primaryKey.getHost(); 
    } 

    public void setHost(String host) { 
     this.primaryKey.setHost(host); 
    } 
// @Column(name = "port", length = 200, nullable = false) 
    public String getPort() { 
     return primaryKey.getPort(); 
    } 

    public void setPort(String port) { 
     this.primaryKey.setPort(port); 
    } 
// @Column(name = "path", length = 200, nullable = false) 
    public String getPath() { 
     return primaryKey.getPath(); 
    } 

    public void setPath(String path) { 
     this.primaryKey.setPath(path); 
    } 

@Embeddable class MediaLocatorPK implements Serializable 
{ 
    private String location; 
    private String description; 
    private String protocol; 
    private String host; 
    private String port; 
    private String path; 

    public String getLocation() { 
     return location; 
    } 
    public void setLocation(String location) { 
     this.location = location; 
    } 
    public String getDescription() { 
     return description; 
    } 
    public void setDescription(String description) { 
     this.description = description; 
    } 
    public String getProtocol() { 
     return protocol; 
    } 
    public void setProtocol(String protocol) { 
     this.protocol = protocol; 
    } 
    public String getHost() { 
     return host; 
    } 
    public void setHost(String host) { 
     this.host = host; 
    } 
    public String getPort() { 
     return port; 
    } 
    public void setPort(String port) { 
     this.port = port; 
    } 
    public String getPath() { 
     return path; 
    } 
    public void setPath(String path) { 
     this.path = path; 
    } 


} 

私は生成されたIDの自動を持っていた、そして各atributeは、例えば、@Column(name = "location", length = 200)、テーブルにcolumを持っていた、前に:これは私がいくつかの例とドキュメントを、次のされた方法です。

私は他のオブジェクトとの関係

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.app.MediaLocator from com.app.MediaObject has the wrong number of column. should be 6 
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:429) 
    at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1443) 
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1262) 
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:693) 
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:628) 
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65) 
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1686) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1393) 
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345) 
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717) 
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1469) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1409) 

から原因referenciesにデータを格納することはできません。このようにして、この特定の関係は次のとおりです。

@ManyToMany(
      cascade={CascadeType.ALL}, 
      fetch=FetchType.LAZY) 
    @JoinTable(name = "media_object_location", 
      [email protected](name="mediaLocator_id"), 
      [email protected](name="mediaObject_id")) 
    public List<MediaObject> getMediaObjects() { 
     return mediaObjects; 
    } 

    public void setMediaObjects(List<MediaObject> mediaObjects) { 
     this.mediaObjects = mediaObjects; 
    } 

そしてメディアオブジェクト側から:

@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY, mappedBy = "mediaObjects") 
    public List<MediaLocator> getMediaLocators() { 
     return mediaLocators; 
    } 

    public void setMediaLocators(List<MediaLocator> mediaLocators) { 
     this.mediaLocators = mediaLocators; 
    } 

何が間違っていますか?あなたがメインクラスからそれらを削除する必要があり、組み込みクラスにプロパティを移動したら、あなたの例では、あなたがすべてを削除する必要があるので、あなたの組み込みクラスのただ一つの基準が存在します、事前

+0

例外はありますか? – mprabhat

+0

ちょうど私の質問を編集した、私は十分なinformaitonを持っていないことを理解した –

+0

あなたは良いデザイン(単一のID列、自動生成)から悪い(すべての列が複合主キーを形成するテーブル)に行った。どうしてそんなことをするのか? MediaLocatorオブジェクトを参照するすべてのエンティティは、独自のテーブルに6つの列を複製する必要があります。また、MediaLocatorの値を変更することはできません。それをしないでください! –

答えて

2

感謝あなたのMediaLocatorクラスの場所のようなプロパティ

MediaLocatorにあるidの物件は何ですか?あなたのクラスに特定の列注釈がないので、hibernateは各属性をテーブル列にマップしようとします。プロパティ列と表列の両方の命名が同じであれば、そのマッピングで成功します。

idが必要ない場合は、削除するか、@Transientとマークしてください。

このコードは、取得したものと同じエラーを再現します。

学生クラス

package com.mumz.test.jpa.embedded.manytomany; 

import java.util.Set; 

import javax.persistence.CascadeType; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.ManyToMany; 
import javax.persistence.Table; 

@Entity 
@Table(name="STUDENT") 
public class StudentMTM { 

    private StudentMTMPK studentPK = null; 
    private Set<Address>  address  = null; 

    public StudentMTM(StudentMTMPK studentPK, Set<Address> address) { 
     super(); 
     this.studentPK = studentPK; 
     this.address = address; 
    } 

    /** 
    * @return the id 
    */ 
    public Integer getId() { 
     return studentPK.getId(); 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(Integer id) { 
     studentPK.setId(id); 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return studentPK.getName(); 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     studentPK.setName(name); 
    } 
    /** 
    * @param studentPK 
    *   the studentPK to set 
    */ 
    public void setStudentPK(StudentMTMPK studentPK) { 
     this.studentPK = studentPK; 
    } 

    /** 
    * @param address 
    *   the address to set 
    */ 
    public void setAddress(Set<Address> address) { 
     this.address = address; 
    } 

    /** 
    * @return the address 
    */ 
    @ManyToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "STUDENT_ADDRESS", joinColumns = 
     { 
      @JoinColumn(name = "STUDENT_ID") 
     }, inverseJoinColumns = 
     { 
      @JoinColumn(name = "ADDRESS_ID") 
     }) 
    public Set<Address> getAddress() { 
     return address; 
    } 

    /** 
    * @return the studentPK 
    */ 
    @Id 
    public StudentMTMPK getStudentPK() { 
     return studentPK; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((studentPK == null) ? 0 : studentPK.hashCode()); 
     result = prime * result + ((address == null) ? 0 : address.hashCode()); 
     return result; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (!(obj instanceof StudentMTM)) { 
      return false; 
     } 
     StudentMTM other = (StudentMTM) obj; 
     if (studentPK == null) { 
      if (other.studentPK != null) { 
       return false; 
      } 
     } else if (!studentPK.equals(other.studentPK)) { 
      return false; 
     } 
     if (address == null) { 
      if (other.address != null) { 
       return false; 
      } 
     } else if (!address.equals(other.address)) { 
      return false; 
     } 
     return true; 
    } 
} 

学生PKクラス:

package com.mumz.test.jpa.embedded.manytomany; 

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Embeddable; 

@Embeddable 
public class StudentMTMPK implements Serializable{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 3686950547855931594L; 
    private Integer id = null; 
    private String name = null; 

    public StudentMTMPK(Integer id, String name) { 
     this.id = id; 
     this.name = name; 
    } 

    /** 
    * @return the id 
    */ 
    @Column(name="STUDENT_ID") 
    public Integer getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    /** 
    * @return the name 
    */ 
    @Column(name="STUDENT_NAME") 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((id == null) ? 0 : id.hashCode()); 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     return result; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (!(obj instanceof StudentMTMPK)) { 
      return false; 
     } 
     StudentMTMPK other = (StudentMTMPK) obj; 
     if (id == null) { 
      if (other.id != null) { 
       return false; 
      } 
     } else if (!id.equals(other.id)) { 
      return false; 
     } 
     if (name == null) { 
      if (other.name != null) { 
       return false; 
      } 
     } else if (!name.equals(other.name)) { 
      return false; 
     } 
     return true; 
    } 
} 

アドレスクラス

package com.mumz.test.jpa.embedded.manytomany; 

import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="ADDRESS") 
public class Address { 
    private Integer id = null; 
    private String addressDetails = null; 

    /** 
    * @return the id 
    */ 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    @Column(name="ADDRESS_ID") 
    public Integer getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    /** 
    * @return the addressDetails 
    */ 
    @Column(name="ADDRESS_DETAILS") 
    public String getAddressDetails() { 
     return addressDetails; 
    } 

    /** 
    * @param addressDetails the addressDetails to set 
    */ 
    public void setAddressDetails(String addressDetails) { 
     this.addressDetails = addressDetails; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((addressDetails == null) ? 0 : addressDetails.hashCode()); 
     result = prime * result + ((id == null) ? 0 : id.hashCode()); 
     return result; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (!(obj instanceof Address)) { 
      return false; 
     } 
     Address other = (Address) obj; 
     if (addressDetails == null) { 
      if (other.addressDetails != null) { 
       return false; 
      } 
     } else if (!addressDetails.equals(other.addressDetails)) { 
      return false; 
     } 
     if (id == null) { 
      if (other.id != null) { 
       return false; 
      } 
     } else if (!id.equals(other.id)) { 
      return false; 
     } 
     return true; 
    } 
} 

あなたは、コードのこの部分を実行して、あなたは意志住所のインスタンスを保存しようget

Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: myJPAService] Unable to configure EntityManagerFactory 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:378) 
    at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:56) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:63) 
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47) 
    at com.mumz.test.jpa.embedded.manytomany.EmbeddableTestMainApp.main(EmbeddableTestMainApp.java:9) 
Caused by: org.hibernate.AnnotationException: A Foreign key refering com.mumz.test.jpa.embedded.manytomany.StudentMTM from com.mumz.test.jpa.embedded.manytomany.Address has the wrong number of column. should be 2 
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:429) 
    at org.hibernate.cfg.annotations.CollectionBinder.bindCollectionSecondPass(CollectionBinder.java:1460) 
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1279) 
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:710) 
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:645) 
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:65) 
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1716) 
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1423) 
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1375) 
    at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1519) 
    at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:193) 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:1100) 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:282) 
    at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:366) 

StudentMTMの私のmanytomanyが壊れています。 - 最初のバージョンからの変更なし

学生クラス

package com.mumz.test.jpa.embedded.manytomany; 

import java.util.Set; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.ManyToMany; 
import javax.persistence.Table; 

@Entity 
@Table(name="STUDENT") 
public class StudentMTM { 

    private StudentMTMPK studentPK = null; 
    private Integer id = null; 
    private Set<Address>  address  = null; 

    public StudentMTM(StudentMTMPK studentPK, Set<Address> address) { 
     super(); 
     this.studentPK = studentPK; 
     this.address = address; 
    } 

    /** 
    * @return the id 
    */ 
    @Column(name="STUDENT_ID") 
    public Integer getId() { 
     return id; 
    } 

    /** 
    * @param id the id to set 
    */ 
    public void setId(Integer id) { 
     this.id = id; 
    } 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return studentPK.getName(); 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     studentPK.setName(name); 
    } 
    /** 
    * @param studentPK 
    *   the studentPK to set 
    */ 
    public void setStudentPK(StudentMTMPK studentPK) { 
     this.studentPK = studentPK; 
    } 

    /** 
    * @param address 
    *   the address to set 
    */ 
    public void setAddress(Set<Address> address) { 
     this.address = address; 
    } 

    /** 
    * @return the address 
    */ 
    @ManyToMany(cascade = CascadeType.ALL) 
    @JoinTable(name = "STUDENT_ADDRESS", joinColumns = 
     { 
      @JoinColumn(name = "STUDENT_ID") 
     }, inverseJoinColumns = 
     { 
      @JoinColumn(name = "ADDRESS_ID") 
     }) 
    public Set<Address> getAddress() { 
     return address; 
    } 

    /** 
    * @return the studentPK 
    */ 
    @Id 
    public StudentMTMPK getStudentPK() { 
     return studentPK; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((studentPK == null) ? 0 : studentPK.hashCode()); 
     result = prime * result + ((address == null) ? 0 : address.hashCode()); 
     return result; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (!(obj instanceof StudentMTM)) { 
      return false; 
     } 
     StudentMTM other = (StudentMTM) obj; 
     if (studentPK == null) { 
      if (other.studentPK != null) { 
       return false; 
      } 
     } else if (!studentPK.equals(other.studentPK)) { 
      return false; 
     } 
     if (address == null) { 
      if (other.address != null) { 
       return false; 
      } 
     } else if (!address.equals(other.address)) { 
      return false; 
     } 
     return true; 
    } 
} 

学生PK

package com.mumz.test.jpa.embedded.manytomany; 

import java.io.Serializable; 

import javax.persistence.Column; 
import javax.persistence.Embeddable; 

@Embeddable 
public class StudentMTMPK implements Serializable{ 
    /** 
    * 
    */ 
    private static final long serialVersionUID = 3686950547855931594L; 
    private String name = null; 

    public StudentMTMPK(String name) { 
     this.name = name; 
    } 

    /** 
    * @return the name 
    */ 
    @Column(name="STUDENT_NAME") 
    public String getName() { 
     return name; 
    } 

    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#hashCode() 
    */ 
    @Override 
    public int hashCode() { 
     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((name == null) ? 0 : name.hashCode()); 
     return result; 
    } 

    /* (non-Javadoc) 
    * @see java.lang.Object#equals(java.lang.Object) 
    */ 
    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) { 
      return true; 
     } 
     if (obj == null) { 
      return false; 
     } 
     if (!(obj instanceof StudentMTMPK)) { 
      return false; 
     } 
     StudentMTMPK other = (StudentMTMPK) obj; 
     if (name == null) { 
      if (other.name != null) { 
       return false; 
      } 
     } else if (!name.equals(other.name)) { 
      return false; 
     } 
     return true; 
    } 
} 

アドレスクラス:それがあるべきよう

今、この作品は動作します。

2番目のバージョンでは、JoinColumnはStudentの一部であり、マッピングはマッピング先を認識しています。

+0

私はすべてのプロパティを組み合わせてIDをします。私はちょうど私のコードの変更で私の質問を編集しました。ありがとう –

+0

あなたの協会manytomanyが壊れていると、あなたの加入列はmediaLocator_idなので、あなたのメディアロケータのどの列を参照しようとしていますか?なし。 – mprabhat

+0

プライベートInteger id = null;学生クラス。そのIDをどうやって生成しますか?自動生成されますか?次に、この2番目のモデルと最初のモデルの違いは何ですか? –

関連する問題