【发布时间】:2017-01-05 09:38:42
【问题描述】:
我在这里提出了很多有用的答案,我希望你也能帮助我解决我的问题。
场景:
我创建了一个带有 servlet 的 JaveEE 动态 Web 项目。 它是一个应用程序管理的持久性项目。像这样 -> https://help.hana.ondemand.com/help/frameset.htm?e4aeacd2bb5710148ee99255136d96a5.html
数据库在 Hana Cloud Platform 上运行,带有试用帐户。
我为复合主键使用了 @IdClass 注释,并将 firstname 和 lastname 标记为 Id。 我用 @Multitenant 和 @TenantDiscriminatorColumn 注释了这个类 我的主键有 3 个值 -> 租户 ID、名字和姓氏。
我还使用 JPAEdmExtension 定义了一些 OData 服务,以通过 OData 请求访问数据。
现在这是我的问题:当我使用 chromes postman 发出 GET 请求时。它运行良好,但是当我尝试 POST 请求时,即使邮递员说 201:已创建,我的数据库中也没有条目
我猜邮递员无法处理作为tenantDiscriminatorColumn且在http请求中不可见的tenant_id列?
但是我该如何解决这个问题呢?我需要使用 OData 访问数据并隐藏租户 ID。
感谢您的回复。
代码附件:
类人:
@Entity
@IdClass(PersonPK.class)
@Table(name = "PERSON")
@Multitenant
@TenantDiscriminatorColumn(length = 36, primaryKey = true)
@NamedQuery(name = "AllPersons", query = "select p from Person p")
public class Person implements Serializable {
@Id
@Column(name = "FIRSTNAME")
private String firstname;
@Id
@Column(name = "LASTNAME")
private String lastname;
//...getter setter constructor
}
类人PK:
public class PersonPK implements Serializable {
private String firstname;
private String lastname;
public PersonPK() { }
public PersonPK(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
//..getter + hashcode / equals override
}
提供实体数据模型的PersonProcessing类:
public class PersonProcessing implements JPAEdmExtension {
@Override
public void extendJPAEdmSchema(JPAEdmSchemaView view) {
Schema edmSchema = view.getEdmSchema();
List<EntityType> entityTypes= edmSchema.getEntityTypes();
for (Iterator<EntityType> iterator = entityTypes.iterator(); iterator.hasNext();) {
EntityType et = (EntityType) iterator.next();
if (et.getName().equals("Person")) {
List<Property> props = et.getProperties();
}
}
}
}
}
Tenantid 由 web.xml 中的资源引用提供
<resource-ref>
<res-ref-name>TenantContext</res-ref-name>
<res-type>com.sap.cloud.account.TenantContext</res-type>
</resource-ref>
使用邮递员获取和发布请求
//..hanatrial.../personApplication/odata.svc/Persons
【问题讨论】:
-
为什么不从调试持久性代码开始呢?并查看 JPA 提供程序日志。
标签: java jpa odata multi-tenant postman