【问题标题】:JPA - Not able to access non entity @mappedsuperclass in manytoone relationshipJPA - 无法在多对一关系中访问非实体@mappedsuperclass
【发布时间】:2012-03-31 03:04:45
【问题描述】:

知道如何在 jpa 中访问多对一关系中的 mappedsuper 类。这是我的代码 sn-p。

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

当我尝试从 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;

}

您能否就这个问题给我任何建议。

【问题讨论】:

    标签: jpa


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢!我又做了一个改变。我将 Message 具体类更改为抽象 Message 类。现在工作正常。
    猜你喜欢
    • 1970-01-01
    • 2013-04-25
    • 2013-01-30
    • 2021-05-25
    • 1970-01-01
    • 2018-05-14
    • 2019-04-07
    • 2017-01-25
    • 2016-10-28
    相关资源
    最近更新 更多