JPA加入了继承和上次修改列问题

时间:2015-05-21 09:15:44

标签: java mysql hibernate jpa

我的所有实体都有一个抽象的超类:

@MappedSuperclass
@Data
@ToString
@EqualsAndHashCode
public abstract class AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@SequenceGenerator(name = "seq")
@Column(name = "ID")
private Long id = 0L;

@CreatedDate
@Generated(GenerationTime.INSERT)
@Column(name = "CREATED", insertable = true, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
private Date created;

@LastModifiedDate
@Version
@Generated(GenerationTime.ALWAYS)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "LAST_MODIFIED", insertable = false, updatable = true)
private Date lastModified;

/**
 * copies the auto generated fields id, created and last modified form the given entity/DTO to this entity/DTO
 * 
 * @param copyEntity the entity to copy from.
 */
public void copy(AbstractEntity copyEntity) {
    this.setId(copyEntity.getId());
    this.setCreated(copyEntity.getCreated());
    this.setLastModified(copyEntity.getLastModified());
}

}

创建的和最后修改的列由DB(MYSQL)生成 当我有一个实体扩展它,并测试se lastModified列更新它是好的。但是,当我有一个实体扩展该抽象实体而另一个实体扩展该实体并且JPA加入了继承时,最后修改的列没有被更新。

超级实体:

@Entity
@Table(name = "OBSERVATIONS")
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING, length = 20)
public class Observation extends AbstractEntity {

private static final long serialVersionUID = 1L;

@Column(name = "OBSERVATION_TIME")
@Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
@JsonSerialize(using = CustomDateTimeSerializer.class)
@JsonDeserialize(using = CustomDateTimeDeserializer.class)
private DateTime observationTime;

}

子实体:

@Entity
@Table(name = "SOIL_MOISTURE_OBSERVATIONS")
@NoArgsConstructor
@AllArgsConstructor
@Data
@EqualsAndHashCode(callSuper = true)
@DiscriminatorValue("SoilMoistureObservation")
public class SoilMoistureObservation extends Observation {

private static final long serialVersionUID = 1L;

@ManyToOne
@JoinColumn(nullable = false, name = "FIELD_ID")
private Field field;

@OneToOne
@JoinColumn(nullable = false, name = "FIELD_EQUIPMENT_ID")
private FieldEquipment fieldEquipment;

@Column(name = "DEPTH")
private float depth;

@ManyToOne
@JoinColumn(nullable = false, name = "DEPTH_UOM_ID")
private UnitOfMeasure depthUOM;

@Column(name = "MOISTURE")
private float moisture;

@ManyToOne
@JoinColumn(nullable = false, name = "MOISTURE_UOM_ID")
private UnitOfMeasure moistureUOM;

public SoilMoistureObservation(Field field, DateTime observationTime, FieldEquipment fieldEquipment, float depth,
        UnitOfMeasure depthUOM, float moisture, UnitOfMeasure moistureUOM) {
    super(observationTime);
    this.field = field;
    this.fieldEquipment = fieldEquipment;
    this.depth = depth;
    this.depthUOM = depthUOM;
    this.moisture = moisture;
    this.moistureUOM = moistureUOM;
}

}

测试用例:

// create entity and check that auto generated fields are null
    SoilMoistureObservation soilMoistureObservation = new SoilMoistureObservation(field, DateTime.now(), fieldEquipment,
            1.0f, unitOfMeasure, 2.0f, unitOfMeasure2);
    assertEquals(new Long(0), soilMoistureObservation.getId());
    assertNull(soilMoistureObservation.getCreated());
    assertNull(soilMoistureObservation.getLastModified());

    // insert the entity and check that auto generated fields are'nt null
    soilMoistureObservation = soilMoistureObservationRepository.save(soilMoistureObservation);
    assertNotEquals(new Long(0), soilMoistureObservation.getId());
    assertNotNull("Pre persist was not called!", soilMoistureObservation.getCreated());
    assertNotNull("Pre persist was not called!", soilMoistureObservation.getLastModified());
    Date firstModifyDate = soilMoistureObservation.getLastModified();

    // check that it's in the db in the record is the same as expected
    // (equals needs to be implemented in entity class)
    SoilMoistureObservation test = soilMoistureObservationRepository.findOne(soilMoistureObservation.getId());
    assertEquals(test, soilMoistureObservation);

    // change the entity and see the its updated
    soilMoistureObservation.setDepth(3.0f);
    soilMoistureObservation.setMoisture(4.0f);
    soilMoistureObservation = soilMoistureObservationRepository.save(soilMoistureObservation);
    test = soilMoistureObservationRepository.findOne(soilMoistureObservation.getId());
    assertEquals(test, soilMoistureObservation);
    testHelper.commitTransaction();
    assertNotEquals("Pre update was not called!", test.getLastModified(), firstModifyDate);

任何帮助将不胜感激,谢谢!

0 个答案:

没有答案