gpt4 book ai didi

java - GWT 中的 hibernate 错误

转载 作者:行者123 更新时间:2023-12-01 15:59:12 27 4
gpt4 key购买 nike

我遇到 hibernate 错误。我只是做一个概念验证来测试 hibernate 中的关联。我几乎可以肯定我认为的错误存在于映射文件中,但我无法弄清楚

我使用的示例是一个用户可以拥有多台计算机,但一台计算机只能供一个用户使用

我的 ModuleLoad 有点类似于 Java 中的 main

public void onModuleLoad() 
{
GreetingServiceAsync S = GWT.create(GreetingService.class);
((ServiceDefTarget) S).setServiceEntryPoint( GWT.getModuleBaseURL() +"greet");

Computer C1= new Computer(1,"ad");
Computer C2= new Computer(2,"ad");
Computer C3= new Computer(3,"ad");
Set <Computer> S1= new HashSet();
S1.add(C2);S1.add(C3);S1.add(C1);

Users U1= new Users(1,S1);
S.greetServer(U1, new AsyncCallback <Users>(){

@Override
public void onFailure(Throwable caught) {
// TODO Auto-generated method stub
Window.alert("Failure");

}

@Override
public void onSuccess(Users result) {
// TODO Auto-generated method stub
Window.alert("Success");
}});

}

用户类:

    import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.sf.gilead.pojo.gwt.LightEntity;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Users extends LightEntity implements IsSerializable
{
private long UserId;
private Set <Computer> Computers=new HashSet <Computer> ();

public Users(){}
public Users(long userId, Set <Computer> computers)
{
UserId = userId;
Computers = computers;
}
public long getUserId() {
return UserId;
}
public void setUserId(long userId) {
UserId = userId;
}
public Set <Computer> getComputers() {
return Computers;
}
public void setComputers(Set<Computer> computers) {
Computers = computers;
}
}

Computer Class:

import net.sf.gilead.pojo.gwt.LightEntity;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Computer extends LightEntity implements IsSerializable
{
private long ComputerId;
private String Description;

public Computer(){}
public Computer(long ComputerId,String Description)
{
this.ComputerId=ComputerId;
this.Description=Description;
}

public long getComputerId() {
return ComputerId;
}
public void setComputerId(long computerId) {
ComputerId = computerId;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}

Users 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">
<!-- Generated Dec 3, 2010 5:21:42 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.domain.Users" table="USERS">
<id name="UserId" type="long">
<column name="USERID" />
<generator class="assigned" />
</id>
<set name="Computers" table="COMPUTER" inverse="false" lazy="true">
<key>
<column name="USERID" />
</key>
<one-to-many class="com.BiddingSystem.domain.Computer" />
</set>
</class>
</hibernate-mapping>

计算机类:

import net.sf.gilead.pojo.gwt.LightEntity;

import com.google.gwt.user.client.rpc.IsSerializable;

public class Computer extends LightEntity implements IsSerializable
{
private long ComputerId;
private String Description;

public Computer(){}
public Computer(long ComputerId,String Description)
{
this.ComputerId=ComputerId;
this.Description=Description;
}

public long getComputerId() {
return ComputerId;
}
public void setComputerId(long computerId) {
ComputerId = computerId;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}

计算机映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 3, 2010 5:21:42 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.domain.Computer" table="COMPUTER">
<id name="ComputerId" type="long">
<column name="COMPUTERID" />
<generator class="assigned" />
</id>
<property name="Description" type="java.lang.String">
<column name="DESCRIPTION" />
</property>
</class>
</hibernate-mapping>

hibernate 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost/postgres</property>
<property name="hibernate.connection.username">postgres</property>
<property name="hibernate.connection.password">noor</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="current_session_context_class">thread</property>
<mapping resource="com/BiddingSystem/domain/Users.hbm.xml"/>
<mapping resource="com/BiddingSystem/domain/Computer.hbm.xml"/>
</session-factory>
</hibernate-configuration>

服务器实现将其保存到数据库:

import java.util.ArrayList;
import java.util.List;

import net.sf.gilead.core.PersistentBeanManager;
import net.sf.gilead.core.hibernate.HibernateUtil;
import net.sf.gilead.core.store.stateless.StatelessProxyStore;
import net.sf.gilead.gwt.PersistentRemoteService;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.BiddingSystem.client.GreetingService;

import com.BiddingSystem.domain.Users;


import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class GreetingServiceImpl extends PersistentRemoteService implements GreetingService
{

private static final long serialVersionUID = 1L;
private HibernateUtil gileadHibernateUtil = new HibernateUtil();
public GreetingServiceImpl ()
{
gileadHibernateUtil.setSessionFactory(com.BiddingSystem.server.HibernateUtil.getSessionFactory());

PersistentBeanManager persistentBeanManager = new PersistentBeanManager();
persistentBeanManager.setPersistenceUtil(gileadHibernateUtil);
persistentBeanManager.setProxyStore(new StatelessProxyStore());

setBeanManager(persistentBeanManager);
}

public Users greetServer(Users S)
{
Session session = gileadHibernateUtil.getSessionFactory().openSession();
Transaction tr= session.beginTransaction();
session.save(S);
tr.commit();
session.close();
return S;
}


}

我收到的错误:

Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:90)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:183)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at com.BiddingSystem.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at net.sf.gilead.gwt.PersistentRemoteService.processCall(PersistentRemoteService.java:174)
... 21 more

用户映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Dec 3, 2010 5:21:42 AM by Hibernate Tools 3.4.0.Beta1 -->
<hibernate-mapping>
<class name="com.BiddingSystem.domain.Users" table="USERS">
<id name="UserId" type="long">
<column name="USERID" />
<generator class="assigned" />
</id>
<set name="Computers" table="COMPUTER" inverse="false" lazy="true">
<key>
<column name="USERID" />
</key>
<one-to-many class="com.BiddingSystem.domain.Computer" />
</set>
</class>
</hibernate-mapping>

另一个文件肯定是好的,因为我已经在没有 hibernate 的情况下测试了它们

如果有人能帮忙,那就太好了

最佳答案

使用 hibernate 中指定的生成器,您需要选择一个不同的未保存值来告诉 hibernate 确定是 INSERT 还是 UPDATE。默认实现会检查 id 列是否为空。由于您是手动分配的,因此即使未保存的实体也会有一个 id,并且 Hibernate 将尝试执行更新而不是插入。

阅读 Hibernate documentation 的 ID 部分(特别是 5.1.2.2.5)

关于java - GWT 中的 hibernate 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4346254/

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