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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:41:39+00:00 2026-05-13T21:41:39+00:00

I have been learning Hibernate for the past few weeks, I have gotten most

  • 0

I have been learning Hibernate for the past few weeks, I have gotten most of what I learned to work but have a question on the efficiency of a One-to-Many mapping. It works, but I am pretty sure that it could be tweaked quite a bit. When saving, I notice that there are three queries that get executed, an insert for the “Parent” object, an insert for the “Child” object and then an update query for the child that updates the foreign key for the parent object. My assumption is that there is a more efficient way to map this relationship so that there is only the two inserts. Am I missing something relatively obvious in my mapping?

Here is my code:

Parent:

@Entity
@Table(name="Punch")
public class Punch implements Serializable
{
    private Long id;
    private DateTime punchDate;
    private Integer userId;
    private List<PunchTimes> punches= new ArrayList<PunchTimes>();
    private static final long serialVersionUID=2010010611;
    ...Various getters & setters...
    @OneToMany
    @JoinColumn(name="punchId_fk")
    @OrderBy("pid")
    @Cascade(org.hibernate.annotations.CascadeType.ALL)
    public List<PunchTimes> getPunches()
    {
        return punches;
    }
}

Child:

@Entity
@Table(name = "PunchTimes")
public class PunchTimes implements Serializable{
    private Long id;
    private Long pid;
    private DateTime inTime;
    private DateTime outTime;
    private Double adjustedTime;
    private static final long serialVersionUID = 20100106;
    private Punch punch;
    ...Various getters & setters...
    @ManyToOne
    @JoinColumn(name = "punchId_fk", insertable = false, updatable = false)
    public Punch getPunch(){
        return punch;
    }
}

SQL Output:

insert into Punch (punchDate, employeeId)
values (?, ?)

insert into PunchTimes (adjusted, inTime, outTime, punchId_fk)
values (?, ?, ?, ?)

update PunchTimes
set punchId_fk=?
where inoutId=?
  • 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-13T21:41:40+00:00Added an answer on May 13, 2026 at 9:41 pm
    import java.io.Serializable;
    import javax.persistence.*;
    import org.hibernate.annotations.Type;
    import org.joda.time.DateTime;
    
    @Entity
    @Table(name = "PunchTimes")
    public class PunchTimes implements Serializable
    {
        private static final long serialVersionUID = 20100106;
        private Long id;
        private Long pid;
        private DateTime inTime;
        private DateTime outTime=null;
        private Double adjustedTime=null;
        private Boolean adjustedByOperator=false;
        private Boolean overtimeAuthorized;
        private Punch punch;
    
        public PunchTimes()
        {
        }
    
        @Column(name = "adjusted")
        public Double getAdjustedTime()
        {
            return adjustedTime;
        }
    
        public void setAdjustedTime(Double adjustedTime)
        {
            this.adjustedTime = adjustedTime;
        }
    
        @Id
        @GeneratedValue
        @Column(name = "inoutId")
        public Long getId()
        {
            return id;
        }
    
        public void setId(Long id)
        {
            this.id = id;
        }
    
        @Column(name = "inTime")
        @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
        public DateTime getInTime()
        {
            return inTime;
        }
    
        public void setInTime(DateTime inTime)
        {
            this.inTime = inTime;
        }
    
        @Column(name = "outTime")
        @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
        public DateTime getOutTime()
        {
            return outTime;
        }
    
        public void setOutTime(DateTime outTime)
        {
            this.outTime = outTime;
        }
    
        @Column(name = "punchId_fk" ,insertable = false, updatable = false)
        public Long getPid()
        {
            return pid;
        }
    
        public void setPid(Long pid)
        {
            this.pid = pid;
        }
    
        @ManyToOne
        @JoinColumn(name = "punchId_fk" )
        public Punch getPunch()
        {
            return punch;
        }
    
        public void setPunch(Punch punch)
        {
            this.punch = punch;
        }
    

    To get rid of the update I had to make the foreign-key (pid) insertable=false, updatable=false

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

Sidebar

Related Questions

I have been learning to use Twitter Bootstrap for the past few weeks and
I have been learning GWT and Hibernate together for the last few weeks and
I have just started learning NHibernate. Over the past few months I have been
I have been learning ASP.NET MVC for a few months. I have learned about
I have been learning C# for the past few days for use with ASP.NET
Have been learning ASP.NET (using C#) over the past few days. I have made
I have been learning Java for a few weeks now and I am really
I have been learning Scala for the past couple of months and now I
I have been learning ASP.NET MVC in the last few months and I think
I have been still learning Hibernate and I need to develope application which can

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.