我们正在尝试使用Spring数据JPA和Spring数据Rest的POC。输出看起来是例外,我们有实体字段的值,除了id,id字段是在抽象类中设置的。
我们正在使用AbstractPeristable的一个简单的实体女巫扩展(它是一个spring-data-jpa类http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/AbstractPersistable.html)。
下面是这一个的源代码:
@MappedSuperclass
public abstract class AbstractPersistable<PK extends Serializable> implements Persistable<PK> {
private static final long serialVersionUID = -5554308939380869754L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
private PK id;
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Persistable#getId()
*/
public PK getId() {
return id;
}
/**
* Sets the id of the entity.
*
* @param id the id to set
*/
protected void setId(final PK id) {
this.id = id;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Persistable#isNew()
*/
public boolean isNew() {
return null == getId();
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return String.format("Entity of type %s with id: %s", this.getClass().getName(), getId());
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (null == obj) {
return false;
}
if (this == obj) {
return true;
}
if (!getClass().equals(obj.getClass())) {
return false;
}
AbstractPersistable<?> that = (AbstractPersistable<?>) obj;
return null == this.getId() ? false : this.getId().equals(that.getId());
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
int hashCode = 17;
hashCode += null == getId() ? 0 : getId().hashCode() * 31;
return hashCode;
}
}
和我们的实体:
@Entity
@Table(name = "TEST_ENTITY")
public class TestEntity extends AbstractPersistable<Long> {
@Column(name = "TITLE", nullable = false, length = 100)
private String title;
@Column(name = "DESCRIPTION", length = 500)
private String description;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "GLOBAL_DATATYPE_ID", referencedColumnName = "ID", nullable = false, updatable = false)
private GlobalDataType dataType;
@Enumerated(EnumType.STRING)
@Column(name = "VALUE_TYPE", nullable = false, length = 30)
private ValueType valueType;
@Enumerated(EnumType.STRING)
@Column(name = "ATTRIBUTE_TYPE", nullable = false, length = 30)
private AttributeType attributeType;
@Column(name = "FE_DISPLAY")
@Convert(converter=BooleanTFConverter.class)
private Boolean feDisplay;
{... getter setter equals hashcode toString ...}
}
然后服务(使用spring-data-rest):
@RepositoryRestResource(path = "test")
public interface TestEntityRepository extends JpaRepository<TestEntity, Long> {
}
我们运行的应用程序与Spring Boot和输出Chrome(URL是http://localhost:8080/test
)是一个JSON HAL数据(id不显示!!!):
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/test{?page,size,sort}",
"templated" : true
}
},
"_embedded" : {
"testEntities" : [ {
"title" : "Test",
"description" : "TEST",
"valueType" : "SINGLE",
"attributeType" : "ATTRIBUTE",
"feDisplay" : true,
"new" : false,
"_links" : {
"self" : {
"href" : "http://localhost:8080/test/1"
},
"dataType" : {
"href" : "http://localhost:8080/test/1/dataType"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
另一件事是当我们在TestEntity上使用AbstractAuditable时(http://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/domain/AbstractAuditable.html),它可以工作,我们从AbstractAuditable中获取字段值,但我们仍然无法获取id。。。
@Entity
@Table(name = "TEST_ENTITY")
public class TestEntity extends AbstractAuditable<User, Long> {
...
}
@MappedSuperclass
public abstract class AbstractAuditable<U, PK extends Serializable> extends AbstractPersistable<PK> implements
Auditable<U, PK> {
private static final long serialVersionUID = 141481953116476081L;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "CREATED_BY", referencedColumnName = "ID", nullable = false, updatable = false)
private U createdBy;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "CREATED_DATE")
private Date createdDate;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "LAST_MODIFIED_BY", referencedColumnName = "ID", nullable = false, updatable = false)
private U lastModifiedBy;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "LAST_MODIFIED_DATE")
private Date lastModifiedDate;
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Auditable#getCreatedBy()
*/
public U getCreatedBy() {
return createdBy;
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.domain.Auditable#setCreatedBy(java.lang.Object)
*/
public void setCreatedBy(final U createdBy) {
this.createdBy = createdBy;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Auditable#getCreatedDate()
*/
public DateTime getCreatedDate() {
return null == createdDate ? null : new DateTime(createdDate);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.domain.Auditable#setCreatedDate(org.joda.time
* .DateTime)
*/
public void setCreatedDate(final DateTime createdDate) {
this.createdDate = null == createdDate ? null : createdDate.toDate();
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Auditable#getLastModifiedBy()
*/
public U getLastModifiedBy() {
return lastModifiedBy;
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.domain.Auditable#setLastModifiedBy(java.lang
* .Object)
*/
public void setLastModifiedBy(final U lastModifiedBy) {
this.lastModifiedBy = lastModifiedBy;
}
/*
* (non-Javadoc)
*
* @see org.springframework.data.domain.Auditable#getLastModifiedDate()
*/
public DateTime getLastModifiedDate() {
return null == lastModifiedDate ? null : new DateTime(lastModifiedDate);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.domain.Auditable#setLastModifiedDate(org.joda
* .time.DateTime)
*/
public void setLastModifiedDate(final DateTime lastModifiedDate) {
this.lastModifiedDate = null == lastModifiedDate ? null : lastModifiedDate.toDate();
}
}
和JSON HAL结果:
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/test{?page,size,sort}",
"templated" : true
}
},
"_embedded" : {
"testEntities" : [ {
"createdBy" : {
"username" : "xxx.admin"
},
"createdDate" : {
"year" : 2014,
"era" : 1,
"dayOfYear" : 282,
"dayOfWeek" : 4,
"dayOfMonth" : 9,
"weekOfWeekyear" : 41,
"monthOfYear" : 10,
"yearOfEra" : 2014,
"yearOfCentury" : 14,
"centuryOfEra" : 20,
"weekyear" : 2014,
"millisOfSecond" : 0,
"millisOfDay" : 65724000,
"secondOfMinute" : 24,
"secondOfDay" : 65724,
"minuteOfHour" : 15,
"minuteOfDay" : 1095,
"hourOfDay" : 18,
"chronology" : {
"zone" : {
"fixed" : false,
"uncachedZone" : {
"cachable" : true,
"fixed" : false,
"id" : "Europe/London"
},
"id" : "Europe/London"
}
},
"zone" : {
"fixed" : false,
"uncachedZone" : {
"cachable" : true,
"fixed" : false,
"id" : "Europe/London"
},
"id" : "Europe/London"
},
"millis" : 1412874924000,
"afterNow" : false,
"beforeNow" : true,
"equalNow" : false
},
"lastModifiedBy" : null,
"lastModifiedDate" : {
"year" : 2014,
"era" : 1,
"dayOfYear" : 282,
"dayOfWeek" : 4,
"dayOfMonth" : 9,
"weekOfWeekyear" : 41,
"monthOfYear" : 10,
"yearOfEra" : 2014,
"yearOfCentury" : 14,
"centuryOfEra" : 20,
"weekyear" : 2014,
"millisOfSecond" : 0,
"millisOfDay" : 65731000,
"secondOfMinute" : 31,
"secondOfDay" : 65731,
"minuteOfHour" : 15,
"minuteOfDay" : 1095,
"hourOfDay" : 18,
"chronology" : {
"zone" : {
"fixed" : false,
"uncachedZone" : {
"cachable" : true,
"fixed" : false,
"id" : "Europe/London"
},
"id" : "Europe/London"
}
},
"zone" : {
"fixed" : false,
"uncachedZone" : {
"cachable" : true,
"fixed" : false,
"id" : "Europe/London"
},
"id" : "Europe/London"
},
"millis" : 1412874931000,
"afterNow" : false,
"beforeNow" : true,
"equalNow" : false
},
"title" : "Test",
"description" : "TEST",
"valueType" : "SINGLE",
"attributeType" : "ATTRIBUTE",
"feDisplay" : true,
"new" : false,
"_links" : {
"self" : {
"href" : "http://localhost:8080/test/1"
},
"dataType" : {
"href" : "http://localhost:8080/test/1/dataType"
}
}
} ]
},
"page" : {
"size" : 20,
"totalElements" : 1,
"totalPages" : 1,
"number" : 0
}
}
编辑:解决方案
添加自定义RepositoryRestMVC配置
@Configuration
public class MyRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {
protected void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.exposeIdsFor(TestEntity.class);
}
}
然后在应用程序java配置类中更改@import
之前:
@Configuration
@EnableTransactionManagement
@Import(RepositoryRestMvcConfiguration.class)
@EnableJpaRepositories(basePackages="uk.co.xxx.marketplace.data.repository")
@PropertySources({@PropertySource("classpath:hibernate.properties"),@PropertySource("classpath:persistence.properties")})
@ComponentScan(value = "uk.co.xxx.marketplace.data")
@EnableAutoConfiguration
public class Application {
...
}
实际:
@Configuration
@EnableTransactionManagement
@Import(MyRepositoryRestMvcConfiguration.class)
@EnableJpaRepositories(basePackages="uk.co.xxx.marketplace.data.repository")
@PropertySources({@PropertySource("classpath:hibernate.properties"),@PropertySource("classpath:persistence.properties")})
@ComponentScan(value = "uk.co.xxx.marketplace.data")
@EnableAutoConfiguration
public class Application {
...
}
您可以对类RepositoryRestConfiguration(或RepositoryRestMVCConfiguration,如果使用Spring MVC)进行子类化,并使用您要公开id的域类调用方法lex eIdsFor()。
问题内容: 有谁知道我该如何在hibernate状态下做到这一点: 使用JPA? EntityManager有一个contains方法,但仅此而已! 我正在编写一些代码,充当实体和会话中存储的数据之间的转换器(因此,不是存储序列化的对象,而是仅存储类名和ID)。 问题答案: 有谁知道我可以用JPA在hibernate(…)中做到这一点? JPA 1.0没有等效的功能,因此如果您坚持使用JPA 1.
我从服务器收到json格式的响应,方法是使用restAsaud使用java点击post Request。 现在我想获取名为“freebies”的子json对象。如何执行此操作,因为在写入时: 我得到了stackoverFlow错误。 请帮帮忙。
问题内容: 但是,首选解决方案(属性访问)在我的情况下不起作用(我缺少列异常-为什么?) 该模型如下所示:实体和。表含有列是的表,以便它是典型的关系。 现在的问题是,如果我取的实体,我需要有机会获得价值(亦称的实体),而不取实体。我怎样才能做到这一点? 我使用的映射如下所示: 我想做的是调用而无需从DB中额外获取实体。 根据我上面提到的答案,如果我将注释从字段移到getter(在实体上,我对吗?)
谢谢,罗伯
我已经使用netbeans(8.0.2)中的持久性单元向导创建了数据源。但在glassfish上运行项目时,它找不到我的数据源。 例外情况 我正在使用EclipseLink JPA 2.1
我在我的项目中使用Spring Data JPA和Hibernate JPA提供程序。在我的服务中,我有一个方法,它将一个实体保存在数据库中,而不是使用返回的对象,我试图获取关于该实体的更多细节。因此,无法获取详细信息。在日志中,我只看到insert语句,而没有select详细信息。 下面是我的代码: 更新:我从服务和测试中删除了@Transactional annotaion。现在,当我保存一个