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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:22:54+00:00 2026-06-16T18:22:54+00:00

Hello stackoverflow community. I want to know, if there is a possibility in OpenJPA

  • 0

Hello stackoverflow community.

I want to know, if there is a possibility in OpenJPA to check first if the object already exists in DB before inserting.

I have two clases:

imports...

@Entity(name="Player")
public class Player {
    private long id;
    private String nickName;
    private Team team; /* A Player is on a Team */

    @ManyToOne(cascade=CascadeType.PERSIST)
    @JoinColumn(name = "team_id")
    public Team getTeam() {
        return team;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

    /* more getters / setters ...*/
}
@Entity(name="Team")
public class Team {
    private long id;
    private String name;
    private List<Player> players; /* A Team Has Players */

    @OneToMany(mappedBy = "team", targetEntity = Player.class, 
            fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    public List<Player> getPlayers() {
        return players;
    }

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

If I want to add a new Player or Team, JPA should check first if this Player or Team is already in the database.
If yes, then it should use the primary / foreign key of existing unit.

Is this possible? Or do I need to write my own method?

Best Regards
Veote

  • 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-16T18:22:56+00:00Added an answer on June 16, 2026 at 6:22 pm

    You may want to check this switch:
    http://openjpa.apache.org/builds/2.2.0/apache-openjpa/docs/jpa_2.2.html#jpa_2.2_cascadePersist

    I guess that you are using OpenJPA 2.2.0.

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

Sidebar

Related Questions

I want to say hello to the stackoverflow community. I've just started using knockout
Hello stackoverflow community. I do not want to draw each pictureBoxes image to Panel1
Hello StackOverflow community, Is there any way to open a folder in Windows Explorer
Hello StackOverflow community, Is there any way to package the installer for an Adobe
Hello StackOverflow, I'd like to know if there is way to trigger an event
Hello StackOverflow community! I started to learn Node.js recently, and decided to implement a
Hello stackoverflow community ! I am trying to figure out how to architect my
Hello StackOverflow community, Using Google App Engine, I wrote a keyToSha256() method within a
Hello again, stackoverflow community! I'm working on writing a simple blog system in Rails,
Hello StackOverflow community, The air.swf file referenced here: http://livedocs.adobe.com/flex/3/html/help.html?content=distributing_apps_3.html used to launch AIR applications

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.