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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:08:17+00:00 2026-05-23T11:08:17+00:00

I’m having a strange problem. I have some records in the database: Company id

  • 0

I’m having a strange problem. I have some records in the database:

Company

  • id = 1, Name = Microsoft
  • id = 2, Name = Sun

Now I have another entity, Event which has a foreign key reference to Company:

@Entity
public class Event {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @ManyToOne
    private Company company;
}

In my web service layer, I create an Event using a company ID passed as a URL param:

@GET
@Path("addEvent")
public String addEvent(@QueryParam("cid") long companyId) {
    Company alreadyInDB = new Company();
    alreadyInDB.setId(companyId);

    Event event = new Event();
    event.setCompany(alreadyInDB);
    eventService.addEvent(event);
}

This than calls the EventService. I originally was using the line of code commented out. But when that failed with the foreign key constraint failure, I added the code to ensure the Company record exists. Sure enough, the code prints out the proper ID. But it still fails with a foreign key constraint violation.

@Service("eventService")
public class EventService {

    @Autowired
    EventDAO eventDAO;

    @Autowired
    CompanyDAO companyDAO;

    @Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
    public void addEvent(Event event) throws Exception
    {
        System.out.println("Company ID: " + event.getCompany().getId());

        Company ensureRecordExists = companyDAO.findById(event.getCompany().getId());
        System.out.println("ensureRecordExists: " + ensureRecordExists.getId());
        event.setCompany(ensureRecordExists);
        //event.setCompany(companyDAO.getReferenceById(event.getCompany().getId()));
        eventDAO.persist(event);
    }
}

Here’s the relevant part of the stacktrace:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (`blah`.`event`, CONSTRAINT `FKE7E9BF09F9DCA789` FOREIGN KEY (`company_id`) REFERENCES `Company` (`id`))
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)

Any idea what’s going on? I can physically see the record exists in mysql workbench. I have no idea why it’s failing. I thought it might have something to do with flushing the session or some transactional issue…? But I see the record…

  • 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-23T11:08:17+00:00Added an answer on May 23, 2026 at 11:08 am

    I narrowed the problem down and posted another question here:

    JPA/Hibernate – Entity name seems to be important. If I rename to "Bob" works fine

    I found the solution here:

    http://developmentality.wordpress.com/2011/05/23/hibernate-mysql-mac-foreign-key-nightmares-a-painless-solution-to-a-painful-problem/

    Due to the combination of mysql, hibernate, and mac os x, there’s some issue where you have to use a lowercase naming strategy in hibernate. Hibernate by default uses a camel case naming strategy.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a reasonable size flat file database of text documents mostly saved in
I have some data like this: 1 2 3 4 5 9 2 6
For some reason, after submitting a string like this Jack’s Spindle from a text
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.