【发布时间】:2017-08-10 01:57:59
【问题描述】:
我正在尝试创建一个 jpa 存储库,但外键主键存在问题。尽管在抽象基类 (MessageDestination) 中指定了它,但它似乎在专用 MessageDestination 类的存储库中是不可见的(例如 MessageDestinationRoom)。
[...] 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“messageDestinationRoomDAO”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException:此类 [class com.chat.message.entity.MessageDestinationRoom] 未定义 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);
}
为了解决这个问题,我看到我可以将MessageDestination 注释为@MappedSuperclass,但这不起作用,因为它需要是@Entity 才能存储在Message 中。可悲的是,这是不可能的:
org.hibernate.AnnotationException: 不能同时使用@Entity 和@MappedSuperclass 注释实体
有什么想法吗?谢谢...
【问题讨论】:
标签: java spring hibernate jpa spring-data-jpa