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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:54:42+00:00 2026-06-18T09:54:42+00:00

I have 2 JPA entities that resemble the following: @Entity class Customer { @Id

  • 0

I have 2 JPA entities that resemble the following:

@Entity
class Customer {
   @Id
   @GeneratedValue
   Long id

   @OneToOne(cascade = CascadeType.ALL)
   @PrimaryKeyJoinColumn
   CustomerInformation customerInformation
}


@Entity
class CustomerInformation {
   @Id
   @OneToOne
   @JoinColumn(name = "id")
   Customer customer

   String firstName
   String lastName
}

I’m using spring-data-jpa to generate my DAO layer. Here it is, though it’s not very interesting:

public interface CustomerRepository extends CrudRepository<Customer, Long> {
}

I’m calling this in a spring context and using @Transactional annotations to tell the JPA provider when to commit the transaction to the database. For testing, I grab the entity manager using @PersistenceContext and manually flush it to end the transaction. Due to the nature of our application, there may be a Customer that exists in the database without a customerInformation object associated with it. If I create a new customer and a customerInformation object in the same transaction, things work as I expect them. For example, this works:

@Transactional
public void createNewCustomer() {
   Customer cust = new Customer();
   CustomerInformation custInf = new CustomerInformation;
   custInf.setCustomer(cust);
   custInf.setFirstName("asdf");
   custInf.setLastName("hjkl");

   cust.setCustomerInformation(custInf);

   customerRepository.save(cust);
}

But if I want to update an existing customer, I run into an issue where it tries to insert the CustomerInformation object with a null id. For example, this fails miserably:

@Transactional
public void updateExistingCustomer(Long userId) {

   Customer foundCustomer = customerRepository.findOne(userId);

   if (foundCustomer.getCustomerInformation() == null) {
      CustomerInformation custInf = new CustomerInformation();
      custInf.setCustomer(foundCustomer);
      custInf.setFirstName("asdf");
      custInf.setLastName("hjkl");

      cust.setCustomerInformation(custInf);

      customerRepository.save(foundCustomer);
   }
}

This fails with the error message:

Hibernate: insert into CustomerInformation (firstName, lastName, id) values (?, ?, ?)
Feb 1, 2013 7:40:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 20000, SQLState: 23502
Feb 1, 2013 7:40:12 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Column 'ID'  cannot accept a NULL value.

Am I misunderstanding something? Any help is appreciated.

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-18T09:54:43+00:00Added an answer on June 18, 2026 at 9:54 am

    I modified the entity to look like this:

    @Entity
    class CustomerInformation {
    
        @Id
        Long id
    
        @MapsId
        @OneToOne
        @JoinColumn(name = "id")
        Customer customer
    
        String firstName
        String lastName
    }
    

    And everything worked. As far as I could tell, both versions of CustomerInformation would have resulted in the same SQL, except the second version models the actual id, which I don’t necessarily need. I’ll expand this in another question, but the above code solved my problem.

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

Sidebar

Related Questions

I have a JPA entity bean similar to the following: @Entity class License {
I'm using jpa and I have the following entity: @Entity @Table(name=favorites_folders) public class FavoritesFolder
I have two JPA entities: @Entity public class TaskSchedule { ... private String name;
Let's say I have two JPA entities: @Entity public class EntityA { @Id private
I have a JPA entity similar to; public class Inventory { private String productname;
A have a JPA entity that has timestamp field and is distinguished by a
I have some integrtion tests that create an HSQL db from JPA entities. This
Here's the facts : I have 2 jars that contain JPA entities in my
This is similar to How to cascade persist using JPA/EclipseLink I have to entities
I have a some JPA entities that inherit from one another and uses discriminator

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.