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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:41:44+00:00 2026-05-24T20:41:44+00:00

I am having problem with my Hibernate Mapping. I have a many to one

  • 0

I am having problem with my Hibernate Mapping.
I have a many to one mapping between my entity.
The two tables in my Oracle DB are the following…

Employee                                                                                                                                                      
--------------------------
EMPLOYEE_ID  
EMPLOYEE_FIRST_NAME   
EMPLOYEE_LAST_NAME   
HEALTH_PLAN_ID                                                                                                                                                   
Health_Plan
------------------=
HEALTH_PLAN_ID
HEALTH_PLAN_NAME
HEALTH_PLAN_RATE

And my mapping…

@Entity
@Table(name = "Employee")
@SequenceGenerator(name = "employee_seq", sequenceName = "employee_seq")
public class Employee {
    private int employeeId;
    private String firstName;
    private String lastName;
    private HealthPlan plan;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "employee_seq")
    @Column(name = "employee_id", nullable = false)
    public int getEmployeeId() {
        return employeeId;
    }
    @Column(name = "employee_first_name", nullable = false)
    public String getFirstName() {
        return firstName;
    }
    @Column(name = "employee_last_name", nullable = false)
    public String getLastName() {
        return lastName;
    }
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "HEALTH_PLAN_ID")
    public HealthPlan getPlan() {
        return plan;
    }
}

@Entity
@Table(name = "HEALTH_PLAN")
@SequenceGenerator(name = "HEALTH_PLAN_SEQ", sequenceName = "HEALTH_PLAN_SEQ")
public class HealthPlan {
    private int healthPlanId;
    private String healthPlanName;
    private double healthPlanRate;
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "HEALTH_PLAN_SEQ")
    @Column(nullable = false, name = "HEALTH_PLAN_ID")
    public int getHealthPlanId() {
        return healthPlanId;
    }
    @Column(nullable = false, name = "HEALTH_PLAN_NAME", unique = true)
    public String getHealthPlanName() {
        return healthPlanName;
    }
    @Column(nullable = false, name = "HEALTH_PLAN_RATE")
    public double getHealthPlanRate() {
        return healthPlanRate;
    }
}

When I ran below code….

    public static void main(String[] args) throws SQLException {        
        HealthPlanDaoImpl healthDao = new HealthPlanDaoImpl();
        EmployeeDaoImpl empDao = new EmployeeDaoImpl();

        HealthPlan plan = new HealthPlan();
        plan.setHealthPlanName("PLAN B");
        plan.setHealthPlanRate(5.0);

        Employee emp = new Employee();
        emp.setPlan(plan);
        emp.setFirstName("Jane");
        emp.setLastName("Doe");
        boolean isSuccess = empDao.addEmployee(emp);
        System.out.println(isSuccess);

    }
public class EmployeeDaoImpl{
    public  boolean addEmployee(Employee emp) {
      boolean bolReturn = true;

      Session session = HibernateUtil.beginTransaction();
      try {
          session.save(emp);
          HibernateUtil.commitTransaction();
      } catch (Exception e) {
          e.printStackTrace();
          bolReturn = false;
          HibernateUtil.rollbackTransaction();
      } 
      return bolReturn;
    }
}

In the Health_Plan Table, the HEALTH_PLAN_ID = 3 (Which is correct!!!)
In the Employee Table, the HEALTH_PLAN_ID = 1850 (Where did this value came from??? I expect this to be 3 also.)

I tried several times and I notice that in the Employee Table HEALTH_PLAN_ID just increment by 300.
I think I already set the cascade option.

Any hints?

  • 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-24T20:41:45+00:00Added an answer on May 24, 2026 at 8:41 pm
    HealthPlan plan = new HealthPlan();
    plan.setHealthPlanName("PLAN B");
    plan.setHealthPlanRate(5.0);
    
    Employee emp = new Employee();
    emp.setPlan(plan);
    emp.setFirstName("Jane");
    emp.setLastName("Doe");
    

    creates a new HealthPlan instance for every Employee instance created. The original HealthPlan instance is not used in a new mapping between a new Employee and an existing HealthPlan. If you query for the existing HealthPlan instance, and then set it to every Employee, you will find that the health plan id will be consistent across objects.

    The discrepancy in the values for the HEALTH_PLAN_ID values can be attributed to the afore mentioned behavior of your test code. Also, the sequence allocation size would have been incremented by 50, which is the default, and you happen to be seeing the result of six consecutive sequence increment operations. Note, that Hibernate will update sequences in a different transaction, so you will find that the sequence values will increment even if you rollback the current transaction.

    On a different note, I would advise against using a unidirectional @ManyToOne relationship with a CascadeType value of ALL. Do you really want to delete the HealthPlan of all Employee instances if one Employee is removed, or for that matter, allow updates of HealthPlan instances from an Employee to be propagated for merge events?

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

Sidebar

Related Questions

I have a Hibernate entity which has one or more <many-to-one mappings, e.g. <hibernate-mapping>
We are having problem with the server migration. We have one application that are
If have an entity A with a bidirectional one-or-zero-to-one mapping with entity B. The
I am having a problem with a Subqueries.exists criteria. I have the following: DetachedCriteria
I'm having a problem implementing a bi-directional parent/child relationship using hibernate 3. The parent,
I'm having problem in the following line: rd.PrintOptions.PaperSize = PaperSize.PaperFanfoldStdGerman; it throws an exception
I'm having problem when running my Windows Forms program. In the program, I have
I'm having some problems with NHibernate when it comes to mapping many to many
Ok so I'm having bit of a problem with my Hibernate mappings and getting
I am having two sets of tables, inherited as follows: Print | +-Magazine -+-

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.