0

theres an exception I cannot get rid of.

I have an entity:

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() {
        [...]
    }
}

With its superclass:

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) {
        [...]
    }
}

With its superclass:

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) {
        [...]
    }   
}

The entity is registered for hibernate (dropwizard style):

private static final HibernateBundle<MyServerConfiguration> hibernate = new HibernateBundle<MyServerConfiguration>(
    MyEntity1.class, MyEntity2.class, [...],
    RawAirPressureData.class) {

@Override
    public DataSourceFactory getDataSourceFactory(MyServerConfiguration configuration) {
        return configuration.getDataSourceFactory();
    }
};

When doing this JUnit:

@Test
    public void testRawAirPressureData() {
        RawAirPressureData myData = TestUtils.generateTestRawAirPressureData();

        RawAirPressureData testMyData = database.inTransaction(() -> {
            return rawAirPressureDataDAO.persist(myData );
        });
[...]

I am getting this Exception:

org.hibernate.MappingException: Unknown entity: mypackage.entities.RawAirPressureData at org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:620) at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1635) at org.hibernate.engine.internal.ForeignKeys.isTransient(ForeignKeys.java:225) at org.hibernate.event.internal.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:499) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:83) at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73) at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:660) at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:652) at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:647) at io.dropwizard.hibernate.AbstractDAO.persist(AbstractDAO.java:202) at mypackage.db.BaseSensorDataDAO.persist(BaseSensorDataDAO.java:33) at mypackage.db.BaseSensorDataDAOTest.lambda$testRawAirPressureData$6(BaseSensorDataDAOTest.java:196)

All previous entities work fine. But none of them consists of mappedsuperclass. I do not understand whats going wrong here.

Any help would be grateful.

Kaspatoo
  • 1,223
  • 2
  • 11
  • 28
  • when I copy (not move) all fields (not the getters) from the grand-superclass (BaseSensorData ) to the superclass (RawSensorData) it is working. Looks like hibernate is not able to support \@mappedsupperclass on a \@mappedsupperclass. – Kaspatoo Jan 09 '19 at 15:03
  • I have to withdraw. It appers to work when I remove the id-generator from the ID field. It worked with previous entities fine, there from I just copied the code. Dont see whats wrong there. – Kaspatoo Jan 09 '19 at 15:27

1 Answers1

0

The problem was that in the Unit Test the dataset was created with giving an ID, although the sequence is gonna be used. Due to that its seems that it stores the recordat first with the given ID and then tries to overwrite it by the sequence which led to to optimisitcLockException.

After erasing the set of the ID in the testdata, the sequence had the way free and it worked.

Kaspatoo
  • 1,223
  • 2
  • 11
  • 28