gpt4 book ai didi

java - 通过hibernate插入2.对象后出现Hsql错误

转载 作者:行者123 更新时间:2023-12-02 08:00:22 24 4
gpt4 key购买 nike

我正在使用hibernate在hsql数据库中插入meal类的对象,如果我只插入1个对象,它工作得很好,但一旦我尝试插入第二个对象,它就会给我一个错误。这是我的代码和错误:

 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>

第一个是主要方法,第二个是要保留的类,第三个是该类的 hibernate 映射。这是错误消息:

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

最佳答案

您声明要为 id 赋值手动分配属性 ( <generator class="assigned" /> ),但实际上并不分配它。

因此,您需要手动分配该值或声明不同的 id 生成策略。

关于java - 通过hibernate插入2.对象后出现Hsql错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com