-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Deleting a Hibernate 3-way Join Entity -Without counterpart
PostPosted: Sun Mar 15, 2015 7:33 pm 
Newbie

Joined: Sun Mar 15, 2015 7:29 pm
Posts: 1
So I am wanting to delete a hibernate 3-way join table entity that I have setup in my project but I keep getting an exception... here is the class I use to map the data into:

Code:
@javax.persistence.Entity(name = "notify_group")
@AssociationOverrides({
    @AssociationOverride(name = "pk.process", joinColumns = @JoinColumn(name = "process_id", referencedColumnName = "id", nullable = false)),
    @AssociationOverride(name = "pk.severity", joinColumns = @JoinColumn(name = "severity_id", referencedColumnName = "id", nullable = false)),
    @AssociationOverride(name = "pk.group", joinColumns = @JoinColumn(name = "group_id", referencedColumnName = "id", nullable = false))
})
@SuppressWarnings("serial")
public class NotifyGroupJoinEntity extends EntityJoin {

    @Embeddable
    public static class PrimaryKey implements EntityPrimaryKey {

        ProcessEntity process;
        SeverityEntity severity;
        LdapBridgeGroupEntity group;

        public PrimaryKey() {}

        public PrimaryKey(ProcessEntity process, SeverityEntity severity,
                          LdapBridgeGroupEntity group) {
            setProcess(process);
            setSeverity(severity);
            setGroup(group);
        }

        @ManyToOne
        public ProcessEntity getProcess() {
            return process;
        }
        public void setProcess(ProcessEntity process) {
            this.process = process;
        }

        @ManyToOne
        public SeverityEntity getSeverity() {
            return severity;
        }
        public void setSeverity(SeverityEntity severity) {
            this.severity = severity;
        }

        @ManyToOne
        public LdapBridgeGroupEntity getGroup() {
            return group;
        }
        public void setGroup(LdapBridgeGroupEntity group) {
            this.group = group;
        }

        @Transient
        public List<Entity> getEntities() {
            List<Entity> objects = new ArrayList<>();

            if(process != null)
                objects.add(process);

            if(severity != null)
                objects.add(severity);

            if(group != null)
                objects.add(group);

            return objects;
        }

        @Override
        public boolean equals(Object o) {
            return equals(o, false);
        }

        public boolean equals(Object o, boolean wildcard) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;

            PrimaryKey that = (PrimaryKey) o;

            if(wildcard) {
                boolean wildCheck = false;
                if(process != null && that.process != null) {
                    if(process.equals(that.process)) wildCheck = true; else wildCheck = false;
                }
                if(severity != null && that.severity != null) {
                    if(severity.equals(that.severity)) wildCheck = true; else wildCheck = false;
                }
                if(group != null && that.group != null) {
                    if(group.equals(that.group)) wildCheck = true; else wildCheck = false;
                }
                return wildCheck;
            }

            if (process != null ? !process.equals(that.process) : that.process != null) return false;
            if (severity != null ? !severity.equals(that.severity) : that.severity != null) return false;
            if (group != null ? !group.equals(that.group) : that.group != null) return false;

            return true;
        }

        @Override
        public int hashCode() {
            int result;
            result = (process != null ? process.getId() : 0);
            result = 31 * result + (severity != null ? severity.getId() : 0);
            result = 31 * result + (group != null ? group.getId() : 0);
            return result;
        }
    }

    private PrimaryKey pk = new PrimaryKey();

    @EmbeddedId
    public PrimaryKey getPk() {
        return pk;
    }

    public void setPk(PrimaryKey pk) {
        this.pk = pk;
    }

    @Override
    @Transient
    public int getId() {
        return getPk().hashCode();
    }

    public void setId(int id) {}

    public NotifyGroupJoinEntity() {}

    public NotifyGroupJoinEntity(ProcessEntity process) {
        this(process, null);
    }

    public NotifyGroupJoinEntity(ProcessEntity process, SeverityEntity severity) {
        this(process, severity, null);
    }

    public NotifyGroupJoinEntity(ProcessEntity process, SeverityEntity severity,
                                 LdapBridgeGroupEntity group) {
        setProcess(process);
        setSeverity(severity);
        setGroup(group);
    }

    @Transient
    public String getCollectionGetter() {
        return "getNotifyGroupJoins";
    }

    @Transient
    public ProcessEntity getProcess() {
        return getPk().getProcess();
    }

    public void setProcess(ProcessEntity process) {
        getPk().setProcess(process);
    }

    @Transient
    public SeverityEntity getSeverity() {
        return getPk().getSeverity();
    }

    public void setSeverity(SeverityEntity severity) {
        getPk().setSeverity(severity);
    }

    @Transient
    public LdapBridgeGroupEntity getGroup() {
        return getPk().getGroup();
    }

    public void setGroup(LdapBridgeGroupEntity group) {
        getPk().setGroup(group);
    }

    @Transient
    public List<Entity> getEntities() {
        return getPk().getEntities();
    }

    @Override
    public boolean equals(Object o) {
        return equals(o, false);
    }

    public boolean equals(Object o, boolean wildcard) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;

        IncidentJoinCauseEntity that = (IncidentJoinCauseEntity) o;

        if (getPk() != null ? !getPk().equals(that.getPk(), wildcard)
            : that.getPk() != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (getPk() != null ? getPk().hashCode() : 0);
    }
}


This works for everything but deleting. I use this Dao method for deleting the class object above:

Code:
@Override
public <E extends Entity> void delete(Class<E> clazz, Serializable id) {
    Session session = getCurrentSession();
    Query query = session.createQuery("delete from " + clazz.getName() + " entity where entity.id = :id");
    query.setSerializable("id", id);
    query.setCacheable(true);
    try {
        query.executeUpdate();
    }
    catch(SerializationException ex) {
        ex.printStackTrace();
    }
}


Then I call this method like this:

Code:
hibernateDao.delete(NotifyGroupJoinEntity.class, joinEntity.getPk());


This is the exception I receive when I make this call in my tests:

Quote:
org.hibernate.exception.GenericJDBCException: could not execute update query
...
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The value is not set for the parameter number 2.


I'm not sure what it is that I'm doing wrong here and would appreciate any help I can get! Thanks for taking the time to read through this issue.

http://stackoverflow.com/questions/29066126/deleting-a-hibernate-3-way-join-entity-without-using-a-hashset


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.