Hallo zusammen,
ich versuche mich zur Zeit etwas in den Java Application Server 9 einzuarbeiten und bin auf ein Problem mit einem selbstreferenzierten Entity-Bean gestoßen.
Ich habe eine Entity GroupEntity, die für eine Benutzergruppe stehen soll. Das Bean hat einen zusammengesetzen Key aus der Group-Id und einer Domain-Id, zu der die Gruppe gehört. Zusätzlich kann eine Gruppe auch eine Vater-Gruppe haben oder selbst Vater mehrere Untergruppen sein.
Hier erstmal der Code, zuerst der Key, dann der Bean
Beim deployen bekomme ich folgende Fehler:
Ich wolte die Parent-Id eigentlich nicht in den Key mit aufnehmen, da es auch eine root-Gruppe ohne Vater möglich sein soll.
Hat vielleicht jemand eine Idee wie ich das realisieren kann oder was falsch ist? Laut einem Tutorial sollte es eigentlich so gehen.
mfg
ich versuche mich zur Zeit etwas in den Java Application Server 9 einzuarbeiten und bin auf ein Problem mit einem selbstreferenzierten Entity-Bean gestoßen.
Ich habe eine Entity GroupEntity, die für eine Benutzergruppe stehen soll. Das Bean hat einen zusammengesetzen Key aus der Group-Id und einer Domain-Id, zu der die Gruppe gehört. Zusätzlich kann eine Gruppe auch eine Vater-Gruppe haben oder selbst Vater mehrere Untergruppen sein.
Hier erstmal der Code, zuerst der Key, dann der Bean
Code:
public class GroupKey implements Serializable {
private Long domainId;
private Long groupId;
public GroupKey() {
}
public Long getDomainId() {
return domainId;
}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
}
Code:
@IdClass(esp.ums.entities.key.GroupKey.class)
@Entity
@Table(name = "ESP_UMS_GROUP")
public class GroupEntity implements Group, Serializable {
private DomainEntity domainEntity;
private GroupEntity parentEntity;
private Collection<GroupEntity> children;
private Long domainId;
private Long groupId;
private String groupName;
private String description;
public GroupEntity() {
}
public GroupEntity(Group group) {
if(group!=null)
BeanHelper.copy(group, this, Group.class);
}
@Transient
public GroupBean getBean() {
return new GroupBean(this);
}
@Id
@Column(name="GROUP_ID")
@GeneratedValue(strategy = GenerationType.TABLE, generator="GROUP_TAB")
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
@Id
@Column(name = "DOMAIN_ID", nullable = false, insertable = false, updatable = false)
public Long getDomainId() {
return domainId;
}
@Transient
public Domain getDomain() {
return getDomainEntity().getBean();
}
public void setDomain(Domain domain) {}
public void setDomainId(Long domainId) {
this.domainId = domainId;
}
@ManyToOne
@JoinColumn(name = "DOMAIN_ID")
public DomainEntity getDomainEntity() {
return domainEntity;
}
public void setDomainEntity(DomainEntity domainEntity) {
this.domainEntity = domainEntity;
}
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@ManyToOne
@JoinColumns({
@JoinColumn(name = "PARENT_GROUP_ID",referencedColumnName = "GROUP_ID")
, @JoinColumn(name = "PARENT_DOMAIN_ID", referencedColumnName = "DOMAIN_ID")
})
public GroupEntity getParentEntity() {
return parentEntity;
}
public void setParentEntity(GroupEntity parent) {
this.parentEntity = parent;
}
}
Beim deployen bekomme ich folgende Fehler:
JDO76609: Got SQLException executing statement "ALTER TABLE ESP_UMS_GROUP ADD CONSTRAINT SPMSGRUPPRNTGRUPID FOREIGN KEY (PARENT_GROUP_ID) REFERENCES ESP_UMS_GROUP (GROUP_ID)": org.apache.derby.client.am.SqlException: Constraint 'SPMSGRUPPRNTGRUPID' is invalid: there is no unique or primary key constraint on table 'ESP.ESP_UMS_GROUP' that matches the number and types of the columns in the foreign key.
JDO76609: Got SQLException executing statement "ALTER TABLE ESP_UMS_GROUP ADD CONSTRAINT SPMSGRPPRNTDMAINID FOREIGN KEY (PARENT_DOMAIN_ID) REFERENCES ESP_UMS_GROUP (DOMAIN_ID)": org.apache.derby.client.am.SqlException: Constraint 'SPMSGRPPRNTDMAINID' is invalid: there is no unique or primary key constraint on table 'ESP.ESP_UMS_GROUP' that matches the number and types of the columns in the foreign key.
Ich wolte die Parent-Id eigentlich nicht in den Key mit aufnehmen, da es auch eine root-Gruppe ohne Vater möglich sein soll.
Hat vielleicht jemand eine Idee wie ich das realisieren kann oder was falsch ist? Laut einem Tutorial sollte es eigentlich so gehen.
mfg