Hibernate,表连接问题(可能)

时间:2012-04-08 19:07:14

标签: hibernate many-to-many

我遇到了GW和Hibernate的问题。

在我的数据库中,我有Sinav(Exam)Soru(Question)个表以及一个名为sinav_soru(Exam_Question)的中间表。当我想使用Hibernate从数据库到达考试列表时,会发生错误。

这是错误明细

   [WARN] Exception while dispatching incoming RPC call
com.google.gwt.user.client.rpc.SerializationException: Type 'org.hibernate.collection.PersistentBag' was not included in the set of types which can be serialized by this SerializationPolicy or its Class object could not be loaded. For security purposes, this type will not be serialized.: instance = [tr.edu.gsu.yds.shared.domain.Soru@307262ee]
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:619)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter$ValueWriter$8.write(ServerSerializationStreamWriter.java:153)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serializeValue(ServerSerializationStreamWriter.java:539)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serializeClass(ServerSerializationStreamWriter.java:709)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serializeImpl(ServerSerializationStreamWriter.java:748)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:621)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
    at com.google.gwt.user.client.rpc.core.java.util.Collection_CustomFieldSerializerBase.serialize(Collection_CustomFieldSerializerBase.java:44)
    at com.google.gwt.user.client.rpc.core.java.util.ArrayList_CustomFieldSerializer.serialize(ArrayList_CustomFieldSerializer.java:39)
    at com.google.gwt.user.client.rpc.core.java.util.ArrayList_CustomFieldSerializer.serializeInstance(ArrayList_CustomFieldSerializer.java:51)
    at com.google.gwt.user.client.rpc.core.java.util.ArrayList_CustomFieldSerializer.serializeInstance(ArrayList_CustomFieldSerializer.java:28)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serializeImpl(ServerSerializationStreamWriter.java:740)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serialize(ServerSerializationStreamWriter.java:621)
    at com.google.gwt.user.client.rpc.impl.AbstractSerializationStreamWriter.writeObject(AbstractSerializationStreamWriter.java:126)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter$ValueWriter$8.write(ServerSerializationStreamWriter.java:153)
    at com.google.gwt.user.server.rpc.impl.ServerSerializationStreamWriter.serializeValue(ServerSerializationStreamWriter.java:539)
    at com.google.gwt.user.server.rpc.RPC.encodeResponse(RPC.java:616)
    at com.google.gwt.user.server.rpc.RPC.encodeResponseForSuccess(RPC.java:474)
    at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:571)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
    at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
    at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

我真的无法解决问题。

这是我的域代码,如下所示:

Sinav.java(考试)

package tr.edu.gsu.yds.shared.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity
@Table(name = "sinav")
public class Sinav implements Serializable
{
    private static final long serialVersionUID = 1L;

    private Integer sinavId;
    private String sinavAd;
    private String eklenmeTarihi;
    private Ogretmen ogretmen;
    private List<Ogrenci> ogrenciler = new ArrayList<Ogrenci>();
    private List<Soru> sorular = new ArrayList<Soru>();

    @Id
    @GeneratedValue( strategy=GenerationType.IDENTITY )
    @Column(name = "sinavId", length=10)
    public Integer getSinavId() {
        return sinavId;
    }

    public void setSinavId(Integer sinavId) {
        this.sinavId = sinavId;
    }

    @Column(name = "sinavAd", nullable = false)
    public String getSinavAd() {
        return sinavAd;
    }

    public void setSinavAd(String sinavAd) {
        this.sinavAd = sinavAd;
    }

    @Column(name = "eklenmeTarihi", nullable = false)
    public String getEklenmeTarihi() {
        return eklenmeTarihi;
    }


    public void setEklenmeTarihi(String eklenmeTarihi) {
        this.eklenmeTarihi = eklenmeTarihi;
    }



    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "ogretmenNo")
    public Ogretmen getOgretmen() {
        return this.ogretmen;
    }

    public void setOgretmen(Ogretmen ogretmen) {
        this.ogretmen = ogretmen;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "sinav")
    public List<Ogrenci> getOgrenciler()
    {
        return ogrenciler;
    }

    public void setOgrenciler( List<Ogrenci> ogrenciler )
    {
        this.ogrenciler = ogrenciler;
    }


    @ManyToMany(fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    @JoinTable(name = "sinav_soru",
        joinColumns = {
            @JoinColumn(name = "sinavId", nullable = false)
        },
        inverseJoinColumns = {
            @JoinColumn(name = "soruId", nullable = false)
        }
    )
    public List<Soru> getSorular() {
        return sorular;
    }

    public void setSorular(List<Soru> sorular) {
        this.sorular = sorular;
    }


}

Soru.java(问题)

package tr.edu.gsu.yds.shared.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "soru")
public class Soru implements Serializable
{
    private static final long serialVersionUID = 4L;

    private Integer soruId;
    private String soruTip;
    private String soruMetin;
    private String soruYardimci;
    private Integer soruPuan;
    private List<Cevap> cevaplar = new ArrayList<Cevap>();
    private List<Sinav> sinavlar = new ArrayList<Sinav>();

    @Id
    @GeneratedValue( strategy=GenerationType.IDENTITY )
    @Column(name = "soruId")
    public Integer getSoruId() {
        return soruId;
    }

    public void setSoruId(Integer soruId) {
        this.soruId = soruId;
    }

    @Column( name = "soruTip", nullable = false, length=20 )
    public String getSoruTip() {
        return soruTip;
    }

    public void setSoruTip( String soruTip ) {
        this.soruTip = soruTip;
    }

    @Column( name = "soruMetin", nullable = false, length = 65535, columnDefinition="Text" )
    public String getSoruMetin() {
        return soruMetin;
    }

    public void setSoruMetin( String soruMetin ) {
        this.soruMetin = soruMetin;
    }

    @Column( name = "soruYardimci", nullable = false, length=45 )
    public String getSoruYardimci() {
        return soruYardimci;
    }

    public void setSoruYardimci( String soruYardimci ) {
        this.soruYardimci = soruYardimci;
    }

    @Column( name = "soruPuan", nullable = false, length=3 )
    public Integer getSoruPuan() {
        return soruPuan;
    }

    public void setSoruPuan( Integer soruPuan ) {
        this.soruPuan = soruPuan;
    }

    @OneToMany( mappedBy = "soru", fetch = FetchType.EAGER, cascade=CascadeType.ALL )
    public List<Cevap> getCevaplar() {
        return cevaplar;
    }

    public void setCevaplar( List<Cevap> cevaplar ) {
        this.cevaplar = cevaplar;
    }

    @ManyToMany( mappedBy = "sorular", fetch = FetchType.LAZY )
    public List<Sinav> getSinavlar() {
        return sinavlar;
    }

    public void setSinavlar(List<Sinav> sinavlar) {
        this.sinavlar = sinavlar;
    }

}

以下是我获得考试清单的代码;

package tr.edu.gsu.yds.client.controller.ogrenci;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

import tr.edu.gsu.yds.client.remote.YdsRemoteService;
import tr.edu.gsu.yds.client.remote.YdsRemoteServiceAsync;
import tr.edu.gsu.yds.client.view.ogrenci.sinavislemleri.OgrenciWritingSoruSayfasi;
import tr.edu.gsu.yds.shared.domain.Sinav;
import tr.edu.gsu.yds.shared.domain.SinavTakvim;
import tr.edu.gsu.yds.shared.domain.Soru;

public class OgrenciWritingSoruSayfasiController
{

    private YdsRemoteServiceAsync   ydsRemoteServiceAsync   = GWT.create( YdsRemoteService.class );
    private OgrenciWritingSoruSayfasi ogrenciWritingSoruSayfasi;

    public OgrenciWritingSoruSayfasiController( OgrenciWritingSoruSayfasi ogrenciWritingSoruSayfasi )
    {
        this.ogrenciWritingSoruSayfasi = ogrenciWritingSoruSayfasi;

    }

    public void send(){

        ydsRemoteServiceAsync.getSinavList( new AsyncCallback<List<Sinav>>()
        {

            @Override
            public void onSuccess( List<Sinav> result )
            {
                for ( Sinav sinav : result )
                {
                    //Just to see , returning results.
                    Window.alert( "sinav " + sinav.getSinavId() );

                }               
            }

            @Override
            public void onFailure( Throwable caught )
            {
                Window.alert( "Failure" );

            }
        } );
        ogrenciWritingSoruSayfasi.getOgrenciWritingWidget().getOkayButton().addClickHandler( new ClickHandler()
        {

            @Override
            public void onClick( ClickEvent event )
            {
                ogrenciWritingSoruSayfasi.getOgrenciWritingWidget().getSoruMetniLabel().setText( "Basıldı" );

            }
        } );


    }

}

3 个答案:

答案 0 :(得分:0)

交换这些:

  private List<Ogrenci> ogrenciler = new ArrayList<Ogrenci>();
private List<Soru> sorular = new ArrayList<Soru>();

使用:

  private Set<Ogrenci> ogrenciler = new HashSet<Ogrenci>();
private Set<Soru> sorular = new HashSet<Soru>();

类似于其他Entity类中的列表。

点击此链接:Serializable Hibernate data object for GWT RPC

答案 1 :(得分:0)

我不确定但是根据我从其他问题的答案中读到的,你忘了添加你的默认(零)参数构造函数。添加此内容后可以再试一次。

答案 2 :(得分:0)

省略这些变量及其各自的方法,它不适用于GWT。

private List<Ogrenci> ogrenciler = new ArrayList<Ogrenci>();
private List<Soru> sorular = new ArrayList<Soru>();

既不是

private Set<Ogrenci> ogrenciler = new HashSet<Ogrenci>();
private Set<Soru> sorular = new HashSet<Soru>();

有不同的解决方案,例如dozer,Gilead等。您也可以使用getbyids方法来获得这些结果以简化。