是我无法摆脱的例外。
我有一个实体:
package mypackage.entities;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "raw_air_pressure")
public class RawAirPressureData extends RawSensorData {
@Override
public String toString() {
[...]
}
}
具有其超类:
package mypackage.entities;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.validation.constraints.NotNull;
@MappedSuperclass
public abstract class RawSensorData extends BaseSensorData {
@Column(name = "timestamp")
@NotNull
private Long timestamp;
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
@Override
public int hashCode() {
[...]
}
@Override
public boolean equals(Object obj) {
[...]
}
}
具有其超类:
package mypackage.entities;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
@MappedSuperclass
public abstract class BaseSensorData {
@Id
@SequenceGenerator(name = "my_seq", sequenceName = "my_seq", allocationSize = 1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="my_seq")
@Column(name = "id")
private Integer id;
@Column(name = "value")
@NotNull
private Float value;
@CreationTimestamp
@Column(name = "created")
@Temporal(TemporalType.TIMESTAMP)
private Date created;
@UpdateTimestamp
@Column(name = "updated")
@Temporal(TemporalType.TIMESTAMP)
private Date updated;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Float getValue() {
return value;
}
public void setValue(Float value) {
this.value = value;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
if(created != null) {
this.created = created;
}
}
public Date getUpdated() {
return updated;
}
public void setUpdated(Date updated) {
if(updated != null) {
this.updated = updated;
}
}
@Override
public int hashCode() {
[...]
}
@Override
public boolean equals(Object obj) {
[...]
}
}
该实体已注册为休眠状态(dropwizard样式):
private static final HibernateBundle<MyServerConfiguration> hibernate = new HibernateBundle<MyServerConfiguration>(
MyEntity1.class, MyEntity2.class, [...],
RawAirPressureData.class) {
@Override
public DataSourceFactory getDataSourceFactory(MyServerConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};
执行此JUnit时:
@Test
public void testRawAirPressureData() {
RawAirPressureData myData = TestUtils.generateTestRawAirPressureData();
RawAirPressureData testMyData = database.inTransaction(() -> {
return rawAirPressureDataDAO.persist(myData );
});
[...]
我收到此异常:
org.hibernate.MappingException:未知实体: mypackage.entities.RawAirPressureData位于 org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620) 在 org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1635) 在 org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:225) 在 org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:499) 在 org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:83) 在 org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) 在 org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:660) 在 org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:652) 在 org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:647) 在io.dropwizard.hibernate.AbstractDAO.persist(AbstractDAO.java:202) 在 mypackage.db.BaseSensorDataDAO.persist(BaseSensorDataDAO.java:33) 在 mypackage.db.BaseSensorDataDAOTest.lambda $ testRawAirPressureData $ 6(BaseSensorDataDAOTest.java:196)
所有先前的实体都可以正常工作。 但是它们都不是由mappingsuperclass组成的。 我不明白这里出了什么问题。
任何帮助将不胜感激。
答案 0 :(得分:0)
问题在于,尽管将使用序列,但在单元测试中,数据集是通过给出ID来创建的。因此,它似乎首先使用给定的ID存储记录,然后尝试按导致optimisitcLockException的顺序覆盖它。
在删除测试数据中的ID集之后,该序列就可以自由运行了。