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

  • Home
  • SEARCH
  • 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 790747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:43:44+00:00 2026-05-14T21:43:44+00:00

I have a simple entity, Code, that I need to persist to a MySQL

  • 0

I have a simple entity, Code, that I need to persist to a MySQL database.

public class Code implements Serializable {

    @Id
    private String key;
    private String description;

    ...getters and setters...
}

The user supplies a file full of key, description pairs which I read, convert to Code objects and then insert in a single transaction using em.merge(code). The file will generally have duplicate entries which I deal with by first adding them to a map keyed on the key field as I read them in.

A problem arises though when keys differ only by case (for example: XYZ and XyZ). My map will, of course, contain both entries but during the merge process MySQL sees the two keys as being the same and the call to merge fails with a MySQLIntegrityConstraintViolationException.

I could easily fix this by uppercasing the keys as I read them in but I’d like to understand exactly what is going wrong. The conclusion I have come to is that JPA considers XYZ and XyZ to be different keys but MySQL considers them to be the same. As such when JPA checks its list of known keys (or does whatever it does to determine whether it needs to perform an insert or update) it fails to find the previous insert and issuing another which then fails. Is this corrent? Is there anyway round this other than better filtering the client data?

I haven’t defined .equals or .hashCode on the Code class so perhaps this is the problem.

  • 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-05-14T21:43:45+00:00Added an answer on May 14, 2026 at 9:43 pm

    I haven’t defined .equals or .hashCode on the Code class so perhaps this is the problem.

    Well, you really should, you don’t want to inherit from the behavior of Object for Entities. Whether you want to use the primary key, do a case sensitive comparison, or use a business identity is another story but you certainly don’t want to use reference equality. You don’t want the following entities:

    Code code1 = new Code();
    code1.setKey("abc");
    
    Code code2 = new Code();
    code2.setKey("abc");
    

    To be considered as different by JPA.

    Second, if you want to be able to insert an Entity with XYZ as key and another one with XyZ, then you should use a case sensitive column type (you can make the varchar column case sensitive by using the binary attribute) or you’ll get a primary key constraint violation.

    So, to summarize:

    • implements equals (and hashCode), decide whether you need case sensitive comparison of the key or not.
    • use the appropriate column type at the database level.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using Entity Frameworks Code First. I have one entity that I need
I'm new to entity frame work code first. I have simple class called Cat
I have simple User entity: public class User { public virtual int Id {
I have a simple code in Entity Framework (EF) v4.1 code first: PasmISOContext db
I have a very simple query: The variable '_dc' is the Entity Framework Code
I have implemented a simple entity ejb with a @version annotation. I expect that
I have a simple ADO.NET Entity Framework 4.0 model (edmx) which defines database tables
I have a simple XCDataModel that contains one Entity with One Attribute. Essentially, I
I have a number of simple controller classes that use Doctrine's entity manager to
I have a bunch of simple entity instances that I have serialized to a

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.