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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:13:56+00:00 2026-06-01T12:13:56+00:00

I am trying to get hibernate to write new objects each time I do

  • 0

I am trying to get hibernate to write new objects each time I do a save as opposed to updating the existing records.

public class Risk {
    private long riskID;
    private Vehicle vehicle;
}

public class Vehicle {  
    private long vehicleID;
    private long riskID;
    private String regNumber;
    private String abiCode;
    private String make;
    private String model;
}

So if a write the risk to the DB. Then I change vehicle on the web and try to save the risk to the DB a second time. I would like to to have two risks in the risk table and two vehicles in the vehicle table.

Currently I am using the hibernate session Save(Object o). This is always creating a new risk in the DB but never creating a new vehicle. It is just updating the original one.

Here is my mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class lazy="false" name="uk.co.test.Risk" schema="quote_engine" table="GV_RISK" >
    <id column="riskID" name="riskID" type="long">
        <generator class="identity"/>
    </id>

    <many-to-one name="vehicle" class="uk.co.test.Vehicle" column="vehicleID" not-null="true" cascade="all" unique="true" />

</class>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="uk.co.test.Vehicle" schema="quote_engine" table="GV_VEHICLE">

    <id name="vehicleID" type="long" column="vehicleID">
        <generator class="identity" />
    </id>

    <property name="regNumber" type="string" column="regNumber" />
    <property name="abiCode" type="string" column="abiCode" />
    <property name="make" type="string" column="make" />
    <property name="model" type="string" column="model" />


</class>

  • 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-01T12:13:57+00:00Added an answer on June 1, 2026 at 12:13 pm

    I think that the simpler way of doing that should be:

    1. Saving the Risk and the Vehicle in one Session.
    2. Just after you close the session, set the IDs to null.
    3. And then, when needed, re-save the objects.

    It’s very important that you close the Hibernate session (use separated session for each save) so that the objects become detached, and when the objects become re attached Hibernates thinks that they are brand new.

    Note: you should change long for Long (the primitive to the wrapper)

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

Sidebar

Related Questions

I am new to hibernate queries, and trying to get a grasp on how
I'm trying to get a collection of objects using Hibernate, but I am not
I'm trying to get started with Hibernate, and when executing my program I get
How to get Hibernate session inside a Hibernate Interceptor? I'm trying to use Hibernate
(see here for the problem I'm trying to solve) How do you get hibernate
I'm using Spring with Hibernate as a JPA provider and are trying to get
I'm trying to get the newest hibernate version for my project, for this purpose
I'm trying to get to grips with what hibernate is doing under the hood,
I've got a hibernate query I'm trying to get working but keep getting an
I have a Spring/JPA/Hibernate application and am trying to get it to pass my

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.