确保额外的外部约束

时间:2012-05-05 23:28:49

标签: orm rdbms jdo

这是我的数据模型。我有

实体 EntityEditor 用户

EntityEditor是一个连接表,它链接到实体和用户,并且还具有角色属性。

我想将以下内容添加到实体

@PersitenceCapable
public class Entity
{
    private String key;
    private Set<EntityEditor> editors;
    private User owner;
}

所有者应该指向在该实体上具有“所有者”角色的用户,即EntityEditor表中应该有一个条目,entity = ourEntityId,user = this guys userid,role =“owner”< / p> 我可以这样做吗?

<field name="owner" persistence-modifier="persistent">
    <foreign-key table="ENTITY_EDITOR" >
        <extension vendor-name="datanucleus" key="relation-discriminator-column" value="role"/>
        <extension vendor-name="datanucleus" key="relation-discriminator-value" value="owner"/>
    </foreign-key>
</field>

或者

<field name="owner" persistence-modifier="persistent" table="ENTITY_EDITOR" mapped-by="editor">
    <join/>
    <element column="USER_ID"/>
    <extension vendor-name="datanucleus" key="relation-discriminator-column" value="role"/>
    <extension vendor-name="datanucleus" key="relation-discriminator-value" value="owner"/>
</field>

根据@DataNucleus的请求,这是我的ddl:

create table USER ( USER_ID int auto_increment primary key);
create table ENTITY ( ENTITY_ID int auto_increment primary key);
create table ENTITY_EDITOR (
  ENTITY_EDITOR_ID int auto_increment primary key,
  USER_ID int not null REFERENCES USER (USER_ID),
  ENTITY_ID int not null REFERENCES ENTITY (ENTITY_ID),
  TYPE varchar(20),
  UNIQUE (USER_ID, ENTITY_ID, TYPE)
);
alter table ENTITY add OWNER int REFERENCES ENTITY_EDITOR (USER_ID);

0 个答案:

没有答案