hibernate在特定请求命令中抛出异常

时间:2017-01-07 17:43:01

标签: java hibernate

我遇到了奇怪的行为。我正在使用休眠。我有两张桌子。

@Entity
public class Nemocnica  implements java.io.Serializable {

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="N_ID")
     private BigDecimal NId;
@Column(name="adresa")
     private String adresa;
@OneToMany
     private Set sanitkas = new HashSet(0);

    public Nemocnica() {
    }


    public Nemocnica( String adresa) {
        this.NId = NId;
    }

    public Nemocnica(BigDecimal NId, String adresa) {
        this.NId = NId;
        this.adresa = adresa;
    }
    public Nemocnica(BigDecimal NId, String adresa, Set sanitkas) {
       this.NId = NId;
       this.adresa = adresa;
       this.sanitkas = sanitkas;
    }

    public BigDecimal getNId() {
        return this.NId;
    }

    public void setNId(BigDecimal NId) {
        this.NId = NId;
    }
    public String getAdresa() {
        return this.adresa;
    }

    public void setAdresa(String adresa) {
        this.adresa = adresa;
    }
    public Set getSanitkas() {
        return this.sanitkas;
    }

    public void setSanitkas(Set sanitkas) {
        this.sanitkas = sanitkas;
    }
}

和第二

@Entity
public class Sanitka  implements java.io.Serializable {


     private BigDecimal sanitkaId;
     private transient Nemocnica nemocnica;
     private BigDecimal kapacita;
     @ManyToOne
     @JoinColumn(name="nemocnica_n_id")
     private Set sanitaris = new HashSet(0);

    public Sanitka() {
    }


    public Sanitka(BigDecimal sanitkaId, Nemocnica nemocnica, BigDecimal kapacita) {
        this.sanitkaId = sanitkaId;
        this.nemocnica = nemocnica;
        this.kapacita = kapacita;
    }
    public Sanitka(BigDecimal sanitkaId, Nemocnica nemocnica, BigDecimal kapacita, Set sanitaris) {
       this.sanitkaId = sanitkaId;
       this.nemocnica = nemocnica;
       this.kapacita = kapacita;
       this.sanitaris = sanitaris;
    }

    public BigDecimal getSanitkaId() {
        return this.sanitkaId;
    }

    public void setSanitkaId(BigDecimal sanitkaId) {
        this.sanitkaId = sanitkaId;
    }
    @ManyToOne(cascade=CascadeType.ALL) 
    public Nemocnica getNemocnica() {
        return this.nemocnica;
    }

    public void setNemocnica(Nemocnica nemocnica) {
        this.nemocnica = nemocnica;
    }
    public BigDecimal getKapacita() {
        return this.kapacita;
    }

    public void setKapacita(BigDecimal kapacita) {
        this.kapacita = kapacita;
    }
    public Set getSanitaris() {
        return this.sanitaris;
    }

    public void setSanitaris(Set sanitaris) {
        this.sanitaris = sanitaris;
    }
}

和他们一起操纵的路线。

nemocnicaRoute:

@Path("nemocnica")
public class nemocnicaRoute {
   // private static final helper db = new helper();
    @GET
    @Path("all")
    @Produces(MediaType.APPLICATION_JSON)
    public String getName(){
        List<Nemocnica> l = db.helper.getNemocnicas();
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        //System.out.println( json );
        //return l.toString();
        return gson.toJson(l);
    }
   // another methods
 }

sanitkaRoute

@Path("sanitka")
public class sanitkaRoute {
    //private static final helper db = new helper();
    @GET
    @Path("all")
    @Produces("text/plain")
    public String getName(){
        List<Sanitka> l = db.helper.getSanitkas();
        GsonBuilder builder = new GsonBuilder();
        Gson gson = builder.create();
        //System.out.println( json );
        //return l.toString();
        return gson.toJson(l);
    }
   // another methods
  }

我正在使用这两个getName()方法从所述表中检索所有数据。

这会引起奇怪的行为,如果我首先在nemocnicaRoute上调用get请求,它的效果会非常好。

然而,当我首先在sanitkaRoute上调用get requet时,它可以正常工作,但之后当我想在nemocniceRoute上调用get请求时它会抛出

  

java.lang.UnsupportedOperationException:尝试序列化   java.lang.Class:org.hibernate.proxy.HibernateProxy。忘了   注册一个类型适配器?

让我感到困惑的是,当我首先在nemocnicaRoute上调用getRequest时,这不会发生。

我怎么能解决这个奇怪的行为?

所有帮助表示赞赏

0 个答案:

没有答案