Skip to Content
0
Former Member
Mar 03, 2009 at 01:00 PM

Referential integrity violated in ManyToMany EJB

49 Views

Referential integrity violated in ManyToMany EJB

Iu2019m using EJB 3 in NWCE 7.1 with entity beans. In a ManyToMany association the Server seems to first delete the main entity and after tries to delete the entry of the join table, which causes a violation of the foreign key on the join table. If I remove the foreign key from the table all works correctly!

Caused by: com.sap.dbtech.jdbc.exceptions.DatabaseException: [350]: Referential integrity violated:FK_USER2GROUP_USER,SP1DB,UM_USER2GROUP at com.sap.dbtech.jdbc.packet.ReplyPacket.createException(ReplyPacket.java:64) at com.sap.dbtech.jdbc.ConnectionSapDB.throwSQLError(ConnectionSapDB.java:959) at com.sap.dbtech.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:597) at com.sap.dbtech.jdbc.ConnectionSapDB.execute(ConnectionSapDB.java:465) at com.sap.dbtech.jdbc.CallableStatementSapDB.execute(CallableStatementSapDB.java:443) at com.sap.dbtech.jdbc.CallableStatementSapDB.execute(CallableStatementSapDB.java:315) at com.sap.dbtech.jdbc.CallableStatementSapDB.executeUpdate(CallableStatementSapDB.java:769) at com.sap.dbtech.jdbc.trace.PreparedStatement.executeUpdate(PreparedStatement.java:81) at com.sap.engine.services.dbpool.wrappers.PreparedStatementWrapper.executeUpdate(PreparedStatementWrapper.java:279) at com.sap.engine.services.orpersistence.core.StoreManager.executeStatement(StoreManager.java:653) at com.sap.engine.services.orpersistence.core.StoreManager.removeEntity(StoreManager.java:572) at com.sap.engine.services.orpersistence.core.PersistenceContextImpl.flush(PersistenceContextImpl.java:284)

I donu2019t need any cascade and cascading doesn't solve the problem (tried it already). I've got a User object which contains a list of Group objects. The Group object doesn't contain any users and adding the User list would cause performance problems:

@Entity
@Table(name = "UM_USER")
public class User implements Serializable {
...

    /**
     * Returns the groups property
     * 
     * @return the groups property
     */
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "UM_USER2GROUP", joinColumns = { @JoinColumn(name = "FK_USER_ID",
    referencedColumnName = "ID") }, inverseJoinColumns = { @JoinColumn(name = "FK_GROUP_NAME",
    referencedColumnName = "NAME") })
    public List<Group> getGroups() {
        return groups;
    }
...
}

The User entity is persisted in the following two tables:

CREATE TABLE UM_USER
(
	ID VARCHAR(64) NOT NULL PRIMARY KEY,
	LOGIN VARCHAR(255),
	ACCOUNT_TYPE VARCHAR(32),
	CONSTRAINT UNQ_USER_LOGIN UNIQUE(LOGIN)
);

CREATE TABLE UM_USER2GROUP
(
	FK_USER_ID VARCHAR(64) NOT NULL,
	FK_GROUP_NAME VARCHAR(128) NOT NULL,
	PRIMARY KEY(FK_USER_ID, FK_GROUP_NAME),
	CONSTRAINT FK_USER2GROUP_USER FOREIGN KEY(FK_USER_ID) REFERENCES UM_USER(ID),
	CONSTRAINT FK_USER2GROUP_GROUP FOREIGN KEY(FK_GROUP_NAME) REFERENCES UM_GROUP(NAME)
);

Is there a way to get this running on NetWeaver CE 7.1 without having to remove the foreign keys?

Regards,

Tarik