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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:53:29+00:00 2026-06-13T08:53:29+00:00

My entities representing a one-to-many: @Entity @Table(name = Users) public class User { @Id

  • 0

My entities representing a one-to-many:

@Entity
@Table(name = "Users")
public class User {
    @Id
    @GeneratedValue
    private int id = -1;

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<SignIn> signIns;
}

@Entity
@Table(name = "SignIns")
public class SignIn {
    @Id
    @GeneratedValue
    private int id;
    @ManyToOne
    private User user;

    public SignIn(User user) {
        this.user = user;
    }
}

When I persist a User and then persist a SignIn with the created User, everything works fine (each table will have 1 row).
When I reuse the created User to persist another sign in, the first sign in row gets replaced by the new sign in.

The code that persists:

User user = new User();
persist(user);
SignIn signIn = new SignIn(user);
SignIn signIn2 = new SignIn(user);
persist(signIn);
persist(signIn2);

private void persist(Object entity) {
        EntityManager em = entityManagerFactory.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        try {
            tx.begin();
            em.merge(entity);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            em.close();
        }
    }

What did I overlook?

  • 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-13T08:53:31+00:00Added an answer on June 13, 2026 at 8:53 am

    I had to set the SignIn on the inverse side too (User in my case).

    So:
    – create a sign in
    – merge it
    – add it the the User
    – merge the User

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

Sidebar

Related Questions

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 two entities A and B. public class A{ @Id @GeneratedValue private Integer
I have two entities: Recipe and Ingredient. Entites: public class Ingredient { public int
I'm deleting entities from a table with a one to many relationship to the
I have two entities with many-to-many relationship defined on them. <set name=TreasuryCodes table=ServiceProviderAccountTreasuryCode lazy=true
My entities look something like that (simplified): public class Person { public Guid Id
I have an entity view model. As there can be many entities in the
My entities are these: public class Customer { public Customer() { Invoices = new
These are my entities: public class Department { public Department() { Employees = new
I have 2 entities: Faculty and Course which are in a one-to-many relationship, that

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.