2017-03-18 13 views
0

jpaリポジトリを作成しようとしていますが、外部キーのプライマリキーに問題があります。抽象基底クラス(MessageDestination)で指定されていますが、特殊なMessageDestinationクラスのリポジトリ(例えばMessageDestinationRoom)のリポジトリからは見えません。名 'messageDestinationRoomDAO' を持つBeanを作成エラー:〜JpaRepositoryの "IdClass not defined"を継承した@OneToOne @Idについて

[...]ネストされた例外はorg.springframework.beans.factory.BeanCreationExceptionがあるinitメソッドの呼び出しに失敗しました。ネストされた例外は、java.lang.IllegalArgumentExceptionがある:このクラス[クラスcom.chat.message.entity.MessageDestinationRoom]私は、私のようにMessageDestinationに注釈を付けることができたことを見た問題を解決するためにIdClass

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class Message implements Serializable {  
    @Id @GeneratedValue(strategy = GenerationType.TABLE) 
    private Long id; 

    @OneToOne(targetEntity = MessageDestination.class, 
       cascade=CascadeType.ALL, mappedBy="msg") 
    @NotNull 
    private MessageDestination dest; 

    //... 
} 

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
public abstract class MessageDestination implements Serializable {  
    @Id @OneToOne(cascade=CascadeType.ALL) 
    private Message msg; 
} 

@Entity 
public class MessageDestinationRoom extends MessageDestination {   
    @OneToOne @NotNull 
    private Room destRoom; 

    //... 
} 

public interface MessageDestinationRoomDAO 
extends JpaRepository<MessageDestinationRoom, Message> { 
    public Set<MessageDestinationRoom> findMessageDestinationRoomByDestRoom 
     (Room dest); 
} 

を定義していません。 @MappedSuperclassが格納されますが、これはMessageに格納するためには@Entityである必要があります。悲しいことに、それは不可能です。

org.hibernate.AnnotationException:エンティティは@Entityと@MappedSuperclass

の両方で注釈を付けることはできません

任意のアイデア?ありがとう...

答えて

0

あなたはクラスごとの継承戦略を使用しているので、マッピングされたスーパークラスはありません(したがって、各エンティティは独自のIDを持つ必要があります)。

MessageDestinationエンティティを@MappedSuperClassとしてアノテーションし、MessageDestinationから@Entityを削除できます。デフォルトでは、各サブクラスは@Idフィールドを含むすべてのフィールドを継承します。

+0

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class MessageDestination implements Serializable { @Id @OneToOne(cascade=CascadeType.ALL) private Message msg; } public interface MessageDestinationRoomDAO extends JpaRepository<MessageDestinationRoom, Message> { public Set<MessageDestinationRoom> findMessageDestinationRoomByDestRoom (Room dest); } 

が、このなります。実際には 'SINGLE_TABLE'戦略ではなく' TABLE_PER_CLASS'を使用します。あなたの提案は次の例外を引き起こします:_org.hibernate.AnnotationException:未知のmappedBy:com.test.Message.dest、未知のプロパティ:com.test.MessageDestination.msg_。ありがとうございます。 :) –

0

私が見つけた唯一の解決策は非常に醜いので、より良い回答のために保留中です。これは、プライマリおよび外部キーを分割で構成され、その冗長性があります...

この:すでに試した

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) 
abstract class MessageDestination implements Serializable {  
    @Id @GeneratedValue(strategy = GenerationType.TABLE) 
    private Long id; 

    @OneToOne(cascade=CascadeType.ALL) 
    private Message msg; 
} 

interface MessageDestinationRoomDAO 
extends JpaRepository<MessageDestinationRoom, Long> { 
    public Set<MessageDestinationRoom> findMessageDestinationRoomByDestRoom 
     (Room dest); 
}