Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8049249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:44:19+00:00 2026-06-05T06:44:19+00:00

I have a Hibernate entity called IssueParticipant. It basically describes the relationship between a

  • 0

I have a Hibernate entity called IssueParticipant. It basically describes the relationship between a user and an issue (which is like a JIRA or Bugzilla issue). It represents a sort of many-to-many linking table in the database, linking a user ID to an issue ID, but also includes other information related to notification settings, and so it is treated as its own entity.

I was having huge problems with using the userId and issueId as a composite key, so I created a synthethic key which is a String (and a varchar in the postgres database), which is formed as: _.

Now, I have a screen where a user can edit all of the users associated with an issue, while also editing the notification settings. In a controller class I create a List of IssueParticipants like this:

IssueParticipant participant = new IssueParticipant();
participant.setUser(accountUser);
participant.setIssue(issue);

So these are of course not managed by Hibernate at this point.

Then in my DAO I iterate through them and call saveOrUpdate(), expecting that if an IssueParticipant with the same synthetic key exists in the database, it will updated; otherwise it will be inserted:

    for (IssueParticipant participant : participants) {
        getCurrentSession().saveOrUpdate(participant);
        savedIds.add(participant.getIssueUserKey());
    }

(savedIds is a List I am maintaining so that I later will know what IssueParticipants I should delete from the database).

Instead of what I expect, though, I get an Exception:

org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "issue_participant_pkey"

Here is my entity class, abbreviated:

public class IssueParticipant extends Entity {

    private String issueUserKey;
    private Long issueId;
    private Long userId;

     // Edit: adding 'dateAdded' definition
    private Date dateAdded;
// ...

    // below may be null
    private SPUser user;
    private Issue issue;

    public static IssueParticipant nulledIssueParticipant() {
        IssueParticipant ip = new IssueParticipant();
        return ip;
    }
    public String getIssueUserKey() {
        return issueUserKey;
    }

    public void setIssueUserKey(String issueUserKey) {
        this.issueUserKey = issueUserKey;
    }

    public Long getId() {
        // currently meaningless
        return 0L;
    }

    public Long getIssueId() {
        return this.issueId;
    }

    public void setIssueId(Long issueId) {
        this.issueId = issueId;
        updateKey();
    }

    public Long getUserId() {
        return this.userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
        updateKey();
    }

    private void updateKey() {
        issueUserKey = getIssueId() + KEY_SEP + getUserId();
    }

    public SPUser getUser() {
        return user;
    }

    public void setUser(SPUser user) {
        this.user = user;
        setUserId(user.getId());
    }

    public Issue getIssue() {
        return issue;
    }

    public void setIssue(Issue issue) {
        this.issue = issue;
        setIssueId(issue.getId());
    }

// edit: adding 'dateAdded' methods
public Date getDateAdded() {
    return dateAdded;
}

public void setDateAdded(Date dateAdded) {
    this.dateAdded = dateAdded;
}

...

}

Here is its hbm file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping default-lazy="false">
    <class name="com.xxx.yyy.IssueParticipant" table="issue_participant">
        <id name="issueUserKey" column="issue_user_key" type="string">
            <generator class="assigned"/>
        </id> 
        <version name="dateAdded" column="date_added" type="timestamp" unsaved-value="null" />
        <property name="issueId" column="issue_id" />
        <many-to-one name="user" column="user_id" class="com.xxx.yyy.SPUser" not-null="true" cascade="none" />
        <property name="alertRss" column="alert_rss" type="boolean" />
        <property name="alertEmail" column="alert_email" type="boolean" />
        <property name="alertWeb" column="alert_web" type="boolean" />
        <property name="alertClient" column="alert_client" type="boolean" />

    </class>
</hibernate-mapping>

And indeed user_issue_key is the primary key in the corresponding database table.

I feel like the right solution might just be to use SpringJDBC in this case, but I’d really love to figure out what’s going on here. Anyone have any thoughts? Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:44:20+00:00Added an answer on June 5, 2026 at 6:44 am

    saveOrUpdate() doesn’t query the database to decide whether it should save or update the given entity. It makes that decision based on the state of the entity, as follows:

    • if the object is already persistent in this session, do nothing
    • if another object associated with the session has the same identifier, throw an exception
    • if the object has no identifier property, save() it
    • if the object’s identifier has the value assigned to a newly instantiated object, save() it
    • if the object is versioned by a <version> or <timestamp>, and the version property value is the same value assigned to a newly instantiated object, save() it
    • otherwise update() the object

    So, as far as I understand in your case decision is based on the value of dateAdded field, therefore you need to keep it to distinguish between new and detached instances.

    See also:

    • 11.7. Automatic state detection
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an entity loaded by Hibernate (via EntityManager ): User u = em.load(User.class,
I'm using Hibernate 3.6.7-Final and Spring 3.0.5. I have entity like this @Entity public
I'm using hibernate, I have a user entity, however I want it to implement
I have a many-to-many relationship between the two entities called events and artists, both
I have a Hibernate class called Expression (simplified here for your viewing pleasure): @Entity
I have hibernate @Entity called Video with fields: @Column(name=TC_IN) private BigDecimal tcIn; @Column(name=TC_OUT) private
I have a strange issue with Hibernate 3.5 which I hope some one can
I am using Hibernate with spring. I have a model-class like this. @Entity @Table(name
I have entity A which has an IList of B called Bs and B
I'm using Hibernate/JPA and have an @Entity object called Order, pointing at a MySQL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.