我创建了一个odata服务 http://localhost:8080/carandodatabaseodata/emplist.svc/ 当我访问上面的URL时,它会显示实体集。
<service xmlns="<LINK>2007/app" xmlns:atom="<LINK>2005/Atom" xml:base="<SAME URL AS ABOVE>carandodatabaseodata/emplist.svc/">
<workspace>
<atom:title>Default</atom:title>
<collection href="ZaeScaleprinters">
<atom:title>ZaeScaleprinters</atom:title>
</collection>
</workspace>
</service>
我也能正确查看元数据信息 以下网址工作正常。 carandodatabaseodata / emplist.svc / $元数据
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="carandodatabaseodata">
<EntityType Name="ZaeScaleprinter">
<Key>
<PropertyRef Name="Printer" />
<PropertyRef Name="Scale" />
<PropertyRef Name="Werks" />
</Key>
<Property Name="Printer" Type="Edm.String" Nullable="false" MaxLength="120" />
<Property Name="Scale" Type="Edm.String" Nullable="false" MaxLength="60" />
<Property Name="Werks" Type="Edm.String" Nullable="false" MaxLength="12" />
</EntityType>
<EntityContainer Name="carandodatabaseodataContainer" m:IsDefaultEntityContainer="true">
<EntitySet Name="ZaeScaleprinters" EntityType="carandodatabaseodata.ZaeScaleprinter" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
但是当我尝试使用时访问实际的实体集 carandodatabaseodata / emplist.svc / ZaeScaleprinters 它给我以下错误
<error xmlns="<LINK>/ado/2007/08/dataservices/metadata">
<code/>
<message xml:lang="en">
"sun.reflect.NativeMethodAccessorImpl: object is not an instance of declaring class. "
</message>
</error>
更新:我也粘贴了java代码。
package com;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
import org.apache.olingo.odata2.processor.api.jpa.ODataJPAServiceFactory;
import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
public class ZAE_SCALEPrinterServiceFactory extends ODataJPAServiceFactory {
private static final String PERSISTENCE_UNIT_NAME = "carandodatabaseodata";
@Override
public ODataJPAContext initializeODataJPAContext()
throws ODataJPARuntimeException {
ODataJPAContext oDatJPAContext = this.getODataJPAContext();
try {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
oDatJPAContext.setEntityManagerFactory(emf);
oDatJPAContext.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
return oDatJPAContext;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
实体类:
package model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The persistent class for the ZAE_SCALEPRINTER database table.
*
*/
@Entity
@Table(name="ZAE_SCALEPRINTER")
public class ZaeScaleprinter implements Serializable {
private static final long serialVersionUID = 1L;
@EmbeddedId
private ZaeScaleprinterPK id;
public ZaeScaleprinter() {
}
public ZaeScaleprinterPK getId() {
return this.id;
}
public void setId(ZaeScaleprinterPK id) {
this.id = id;
}
}
package model;
import java.io.Serializable;
import javax.persistence.*;
/**
* The primary key class for the ZAE_SCALEPRINTER database table.
*
*/
@Embeddable
public class ZaeScaleprinterPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
@Column(unique=true, nullable=false, length=12)
private String werks;
@Column(unique=true, nullable=false, length=60)
private String scale;
@Column(unique=true, nullable=false, length=120)
private String printer;
public ZaeScaleprinterPK() {
}
public String getWerks() {
return this.werks;
}
public void setWerks(String werks) {
this.werks = werks;
}
public String getScale() {
return this.scale;
}
public void setScale(String scale) {
this.scale = scale;
}
public String getPrinter() {
return this.printer;
}
public void setPrinter(String printer) {
this.printer = printer;
}
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (!(other instanceof ZaeScaleprinterPK)) {
return false;
}
ZaeScaleprinterPK castOther = (ZaeScaleprinterPK)other;
return
this.werks.equals(castOther.werks)
&& this.scale.equals(castOther.scale)
&& this.printer.equals(castOther.printer);
}
public int hashCode() {
final int prime = 31;
int hash = 17;
hash = hash * prime + this.werks.hashCode();
hash = hash * prime + this.scale.hashCode();
hash = hash * prime + this.printer.hashCode();
return hash;
}
}