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 5974445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:56:40+00:00 2026-05-22T20:56:40+00:00

I have a table with 2 primary keys (so the combination of both of

  • 0

I have a table with 2 primary keys (so the combination of both of them should be unique). The schema is like this:

TABLE="INVOICE_LOGS"
INVOICE_NUMBER  PK  non-null
INVOICE_TYPE    PK  non-null
AMOUNT  

When I try to persist multiple records at a time, I get this hibernate exception:

Caused by:
javax.persistence.PersistenceException:
org.hibernate.NonUniqueObjectException:
a different object with the same
identifier value was already
associated with the session:
[…InvoiceLog#110105269]

So if I use the following code below, I get the hibernate exception above. It works fine if I only persist one record but fails if I try to persist a collection like what I’m trying to do.

    List<InvoiceLog> logs = getLogs();
    for(OvercounterLogDO log: logs) {
        persist(log);
    }

    public void persist(final Object entity) {
        entityManager.persist(entity);
        entityManager.flush();
    }

The hibernate mapping class is below:

@Component(InvoiceLog.BEAN_NAME)
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Entity
@Table(name = "INVOICE_LOGS", uniqueConstraints = {@UniqueConstraint(columnNames = "INVOICE_NUMBER"),@UniqueConstraint(columnNames = "INVOICE_TYPE")} ) 
public class InvoiceLog implements java.io.Serializable {

    private static final long serialVersionUID = -7576525197897271909L;

    protected static final String BEAN_NAME = "invoiceLog";

    private long invoiceNumber;
    private String invoiceType;
    private Double amount;

    public InvoiceLog(){}

    public InvoiceLog(Integer invoiceNumber, String invoiceType,
            Double amount) {
        super();
        this.invoiceNumber = invoiceNumber;
        this.invoiceType = invoiceType;
        this.amount = amount;
    }

    @Id
    @Column(name = "INVOICE_NUMBER", unique = true, nullable = false, precision = 10, scale = 0)
    public long getInvoiceNumber() {
        return invoiceNumber;
    }
    public void setInvoiceNumber(long invoiceNumber) {
        this.invoiceNumber = invoiceNumber;
    }

//  @Id
    @Column(name = "INVOICE_TYPE", nullable = false, length = 4)
    public String getInvoiceType() {
        return invoiceType;
    }
    public void setInvoiceType(String invoiceType) {
        this.invoiceType = invoiceType;
    }

    @Column(name = "AMOUNT", nullable = false, length = 12)
    public Double getAmount() {
        return amount;
    }
    public void setAmount(Double amount) {
        this.amount = amount;
    }

}
  • 1 1 Answer
  • 2 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-05-22T20:56:40+00:00Added an answer on May 22, 2026 at 8:56 pm

    Your current mapping has a single Id, invoiceNumber, not a compound key. As such, it’ll demand that the invoiceNumber be unique, not the combination you want. That’s what the exception is telling you, it’s noticing you’re trying to save a second record with the same Id value.

    You need to map the two fields as a composite key. It looks from your commenting out the second @Id that you were going in that direction at some point. Here’s the documentation on methods for mapping the composite key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with primary keys that look like this: FIRSTKEY~ABC SECONDKEY~DEF FIRSTKEY~DEF
I have a table structured as follows: table(A, B) They are both primary keys
I have a table with composite primary keys. This is a cross reference table
i have a Dimension Table like this : my_Table pk1 Primary key pk2 Primary
I have a table with two primary keys,how can i map the same in
I have table with a unique auto-incremental primary key. Over time, entries may be
I have a table that has, among its primary keys, a VARCHAR(16) column that
My question is about indexes and primary keys. Let's suppose we have a table
I have a table like this CREATE TABLE values ( id int(10) auto_increment NOT
I have a table with 2 primary keys (int, int) and was wondering if

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.