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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:04:25+00:00 2026-06-03T23:04:25+00:00

@Entity public class Troop { @OneToMany(mappedBy=troop) public Set<Soldier> getSoldiers() { … } @Entity public

  • 0
@Entity
public class Troop {
    @OneToMany(mappedBy="troop")
    public Set<Soldier> getSoldiers() {
    ...
}

@Entity
public class Soldier {
    @ManyToOne
    @JoinColumn(name="troop_fk")
    public Troop getTroop() {
    ...
}       

I am struggling with documentation on this:

Troop has a bidirectional one to many relationship with Soldier through the troop property. You don't have to (must not) define any physical mapping in the mappedBy side.

So for example the following code:

Troup t = new Troup();
t.getSoldiers().add(soldier);

What would the difference be if I just called session.saveOrUpdate(t), and if I just called
session.saveOrUpdate(s)? MappedBy defines troup as the owner, but what specifically does this mean? Because I would expect that if I save the soldier object, surely the troop_fk column will be saved correctly? And if I just save the troup object, surely the the soldier foreign key will still be update correctly when cascading? I really can’t see the difference.

  • 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-03T23:04:26+00:00Added an answer on June 3, 2026 at 11:04 pm

    the owner is the entity which sets the foreign key in the database on flushing.

    the code:

    Troup t = new Troup();
    t.getSoldiers().add(soldier);
    session.SaveOrUpdate(t);
    session.Flush();
    

    without cascading:

    throws references transient instances
    

    with cascading and owner = troop

    INSERT INTO troops (id, ...) VALUES (1, ...)
    INSERT INTO soldiers (..., troop_fk) VALUES (..., NULL)
    UPDATE soldiers SET troop_fk=1    <- troop sets its key
    

    with cascading and owner = soldier

    INSERT INTO troops (id, ...) VALUES (1, ...)
    INSERT INTO soldiers (..., troop_fk) VALUES (..., 1) <- soldier saves the reference
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have something like this @Entity public class Facility { ... @ManyToOne @JoinColumn(name=CUSTOMER_FK)
My entities: @Entity public class User implements Serializable { @OneToMany(orphanRemoval=true,cascade=CascadeType.ALL) @JoinColumn(name=_user_id) @MapKey(name=quest) private Map<String,
I have the following entities: @Entity public class Owner{ @Id @Column(name = OWNER_ID) @OneToMany()
These are my entities @Entity public class User { @ManyToMany(mappedBy=admins) private Set<Game> adminForGames =
I have two entities @Entity public class Tabulka{ @OneToMany(mappedBy = tabulka) private List<VysledkyHraca> vysledkyHraca;
Given a simple entity relationship: @Entity public class Single { @OneToMany public Set<Multiple> multiples;
@Entity public class Master implements Serializable{ private List slaves; @OneToMany(mappedBy = Master,fetch=FetchType.LAZY) public List
I have this entity: public class Product { public string Name { get; set;
@Entity public class Person { @ElementCollection @CollectionTable(name = PERSON_LOCATIONS, joinColumns = @JoinColumn(name = PERSON_ID))
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)

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.