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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:23:40+00:00 2026-05-28T08:23:40+00:00

I am using hibernate to insert objects of the class meal in a hsql

  • 0

I am using hibernate to insert objects of the class meal in a hsql DB It works fine if I only insert 1 object but as soon as I try to insert a second it gives me an error. Here is my code and the error:

 public static void main(String[] args) {
    Main obj = new Main();
    Long mealId1 = obj.saveMeal("Pommes");
    Long mealId2 = obj.saveMeal("Doener1");
}

public Long saveMeal(String mealName)
{

    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    Long mealId = null;
    try {
        transaction = session.beginTransaction();
        Meal meal = new Meal(mealName);
        mealId = (Long) session.save(meal);
        transaction.commit();
    } catch (HibernateException e) {
        transaction.rollback();
        e.printStackTrace();
    } finally {
        session.close();
    }
    return mealId;
}


package data;
import java.util.List;
import java.util.LinkedList;

public class Meal implements java.io.Serializable {

private long id;
private String name;
private List<Image> images;
private List<Review> reviews;
private Grouping grouping;

public Meal() {
}

public Meal(String mealName) {
    name = mealName;

}

public Meal(long mealId, String mealName) {
    id = mealId;
    name = mealName;
}

public long getId() {
    return id;
}

public void setId(long mealId) {
    id = mealId;
}

public String getMealName() {
    return name;
}

public void setMealName(String mealName) {
    name = mealName;
}

public float getAvg() {
    float avg = 0;
    for (int i = 0; i < reviews.size(); i++)
    {
        avg = avg + reviews.get(i).getReviewPoints();
    }
    return avg;
}

public List<Image> getNumberImages(int number) {
    assert (number >= 0);
    return  images.subList(0, number) ;
}

public List<Image> getImages() {
    return images;
}

public void setImages(LinkedList<Image> images) {
    this.images = images;
}



public List<Review> getReviews(int number) {
    assert (number >= 0);
    return  reviews.subList(0, number) ;
}



public LinkedList<String> getAltNames() {
    LinkedList<String> altNames = new LinkedList<String>();
    LinkedList<Meal> altNameMeals = grouping.getMeals();
    for (int i = 0; i < altNameMeals.size(); i++)
    {
        altNames.add(altNameMeals.get(i).getMealName());
    }
    return altNames;
}

public void addReview(Review review) {
    if (!reviews.contains(review)) {
        reviews.add(review);
    }
}

public Grouping getGrouping() {
    return grouping;
}

public void setGrouping(Grouping grouping) {
    this.grouping = grouping;
}


public void addImage(Image image) {
    if (!images.contains(image)) {
        images.add(image);
    }
}<hibernate-mapping>
<class name="data.Meal" table="MEAL">
    <id name="id" type="long" access="field">
        <column name="ID" />
        <generator class="assigned" />
    </id>
    <property name="name" type="java.lang.String" access="field">
        <column name="NAME" />
    </property>


    <list name="images" inverse="false" table="IMAGE" lazy="true" access="field">
        <key>
            <column name="ID" />
        </key>
        <list-index column="column_name" />
        <one-to-many class="data.Image" />
    </list>
    <list name="reviews" inverse="false" table="REVIEW" lazy="true" access="field">
        <key>
            <column name="ID" />
        </key>
        <list-index></list-index>
        <one-to-many class="data.Review" />
    </list>


    <many-to-one name="grouping" class="data.Grouping" fetch="join">
        <column name="GROUPING" />
    </many-to-one>

</class>

First is the main method 2nd the class to be persitet and 3rd the hibernate mapping for the class. This is the error message:

843 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: -104, SQLState: 23505  
843 [main] ERROR org.hibernate.util.JDBCExceptionReporter - integrity constraint violation: unique constraint or index violation; SYS_PK_10585 table: MEAL  
843 [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
  • 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-28T08:23:41+00:00Added an answer on May 28, 2026 at 8:23 am

    You declared that you want to assign values to id property manually (<generator class="assigned" />), but don’t actually assign it.

    So, you need to assign the value manually or declare different id generation strategy.

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

Sidebar

Related Questions

I have a DAO class which I'm using to try select/update/insert with hibernate and
I'm trying to insert some new objects into a firebird database using NHibernate. I
Using hibernate, how can I persist a class with a List<String> field? Consider the
I'm having problems using Hibernate and SQL Server 2008. When I try to save
firstly please excuse my relative inexperience with Hibernate I’ve only really been using it
I want to save some Objects to database with predefined IDs using Hibernate. Is
Recently i am started using hibernate. to insert or update i am using saveOrUpdate()
I am using hibernate to map my classes to oracle database. But when I
I'm looking for a way to map a simple insert stored procedure using NHibernate
when using hibernate with spring, can someone explain how the session unit of work

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.