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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:40:52+00:00 2026-05-23T15:40:52+00:00

I have three classes: Location, MTFCC, and BorderPoint. Location has a unidirectional @ManyToOne relationship

  • 0

I have three classes: Location, MTFCC, and BorderPoint.

Location has a unidirectional @ManyToOne relationship with MTFCC, which is intended only as a Lookup table. No cascading is defined.

Location also has a bidirectional @ManyToOne/@OneToMany with BorderPoint. Since I want all associated BorderPoint objects to delete when I delete a Location, I set cascadetype.ALL on the Location side of the relationship.

Unfortunately, an EntityExistsException is being thrown when I attempt to delete a location:

org.apache.openjpa.persistence.EntityExistsException: Cannot delete or update 
a parent row: a foreign key constraint fails (`mapmaker`.`BORDERPOINT`, 
CONSTRAINT `BORDERPOINT_ibfk_1` FOREIGN KEY (`LOCATIONID`) REFERENCES `LOCATION`
(`LOCATIONID`)) {prepstmnt 21576566 DELETE t0, t1 FROM LOCATION t0 INNER JOIN 
MTFCC t1 ON t0.MTFCCID = t1.MTFCCID WHERE (t0.STATEFP = ? AND t1.MTFCCCODE = ?)
[params=?, ?]} [code=1451, state=23000]

[ERROR] FailedObject: DELETE t0, t1 FROM LOCATION t0 INNER JOIN MTFCC t1 ON 
t0.MTFCCID = t1.MTFCCID WHERE (t0.STATEFP = ? AND t1.MTFCCCODE = ?) 
[java.lang.String]

It looks like it’s attempting to delete the associated MTFCC object which I do NOT want to happen. I do, however, want the associated BorderPoint objects to be deleted.

Here is the code (chopped down a bit):

@SuppressWarnings("unused")
@Entity
@Table(name="LOCATION")
@DetachedState(enabled=true)
public class Location implements Serializable, IsSerializable, Cloneable {

private Long id;
private String stateGeoId;
private MTFCC mtfcc;
private List<BorderPoint> borderPointList;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="LOCATIONID")
public Long getId() {
    return id;
}

@ManyToOne
@JoinColumn(name="MTFCCID")
public MTFCC getMtfcc() {
    return mtfcc;
}

@OneToMany(cascade = CascadeType.ALL, mappedBy = "location", fetch = FetchType.EAGER)
public List<BorderPoint> getBorderPointList() {
    return borderPointList;
}

}

@Entity
@Table(name = "BORDERPOINT")
@DetachedState(enabled = true)
public class BorderPoint implements Serializable, IsSerializable {

private Long id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="BORDERID")
public Long getId() {
    return id;
}

@ManyToOne(targetEntity = Location.class)
@JoinColumn(name="LOCATIONID")
public Location getLocation() {
    return location;
}

}

@Entity
@Table(name = "MTFCC")
public class MTFCC implements Serializable, IsSerializable {

    private Long id;
    private String mtfccCode;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "MTFCCID")
public Long getId() {
    return id;
}

// etc

}

And, for good measure, here is the deletion code:

@Override
@Transactional
public int removeByStateGeoIdAndMtfcc(String stateGeoId, String mtfccCode) throws RepositoryException {

    EntityManager em = entityManagerFactory.createEntityManager();
    String jpaQuery = "DELETE FROM Location L where L.stateFP = ?1 AND L.mtfcc.mtfccCode = ?2";
    int affectedRows = 0;
    Query query = em.createQuery(jpaQuery).setParameter(1, stateGeoId).setParameter(2, mtfccCode);

    try {
        em.getTransaction().begin();
        affectedRows = query.executeUpdate();
        em.getTransaction().commit();
    } catch (Exception e) {
        //log.debug("Exception: ", e);
        throw new RepositoryException(e);
    }
    em.close();

    return affectedRows;
}

Hopefully I copied all relevant parts… can anyone assist?

  • 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-23T15:40:52+00:00Added an answer on May 23, 2026 at 3:40 pm

    You aren’t reading the error message correctly. It says that the deletion is forbidden because of the foreign key constraint between BorderPoint and Location.

    The cascade delete would work if you used em.remove(location) to delete your Location. Using a delete query like you’re doing won’t automagically delete the BorderPoints before deleting the location.

    Either load them and remove them using em.remove, or execute other delete queries before to delete the BorderPoints.

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

Sidebar

Related Questions

I have three (C++) classes: Player, Hand, and Card. Player has a member, hand,
I have a three nested classes, Show, Season and Episode, where a show has
Suppose I have three classes. It is valid to instantiate A, but there are
Say I have three classes: class X{}; class Y{}; class Both : public X,
If I have three classes, A, B, C. A and B are friends (bidirectionally).
I have created a debugger visualizer in VS2008. There are two classes i've made,
I have the following 3 classes Book Product SpecialOptions There are many Books, and
is there any way to make IE6 understand double classes, say I have a
Let's say I have a Base class and several Derived classes. Is there any
I have three tables: page, attachment, page-attachment I have data like this: page ID

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.