JPA - 无法在多人关系中访问非实体@mappedsuperclass

时间:2012-03-14 16:08:22

标签: jpa

任何想法如何在jpa中的manytoone关系中访问mappedsuper类。这是我的代码片段。

消息是非实体映射的超类(没有消息表) SystemMessage是一个实体类表名,是SYSTEM_MESSAGE // union子类 OrganizationMessage是一个实体类表名,是ORGANIZATION_MESSAGE // union subclass

当我尝试从ORGANIZATION_NOTIFICATION访问组织通知域对象时 组织通知表有message_id列(Message)多个关系。

它没有加载,它抛出的消息不是实体。即使我尝试@entity而不是@mappedsuperclass但它没有加载消息。我应该通过组织通知

访问系统消息和组织消息
@MappedSuperClass
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS )
public class Message extends BaseDomain implements Comparable<Message>
{
  private final static Logger log = Logger.getLogger( Message.class );

  @Id
  @Column( name = "MESSAGE_ID" )
  @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "MESSAGE_PK_SQ" )
  @SequenceGenerator( name = "MESSAGE_PK_SQ", sequenceName = "MESSAGE_PK_SQ",       allocationSize = 1 )

  private Long id;
  @Column(name="MESSAGE_NAME")
  private String name;
}

@Entity
@Table( name = "SYSTEM_MESSAGE" )
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
public class SystemMessage extends Message
{
//some persitant variables
}


@Entity
@Table( name = "ORGANIZATION_MESSAGE" )
@Inheritance( strategy = InheritanceType.SINGLE_TABLE )
public class OrganizationMessage extends Message
{
//some persitant variables
}


@SuppressWarnings( "serial" )
@Entity
@Table( name = "ORGANIZATION_NOTIFICATION" )
public class OrganizationNotification extends BaseDomain implements Comparable<OrganizationNotification>
{
@Id
  @Column( name = "ORGANIZATION_NOTIFICATION_ID" )
  @GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "org_notification_pk_sq" )
  @SequenceGenerator( name = "org_notification_pk_sq", sequenceName = "org_notification_pk_sq", allocationSize = 1 )
  private Long id;


 //Here is the issue. when i try to access message it says it is not entity otherwise it is not loading if i use entity in message and not saving. 
 @ManyToOne( targetEntity = Message.class )
  @JoinColumn( name = "MESSAGE_ID", insertable = true, updatable = true, nullable = false,referencedColumnName="MESSAGE_ID" )
  private Message message;

}

请您就此问题向我提出任何建议。

1 个答案:

答案 0 :(得分:3)

另一个实体与Message实例有关联的事实证明Message不能是MappedSuperclass,而是实体。

使用@Entity而不是@MappedSuperclass注释消息。

MappedSuperclass用于继承没有其他共同点的实体中的字段和/或关联。