2

当我使用 JPA 持久化实体时,我的版本列没有得到更新/设置。我正在使用休眠 jpa。版本是自动创建的还是我必须手动创建?

我有一个抽象基类

@MappedSuperclass 
public abstract  class AbstractDomainEO {

private Date createdDate;
@Version 
private Integer version;

public Integer getVersion() {
    return version;
}

public void setVersion(Integer version) {
    this.version = version;
}

以及扩展抽象类的子实体类

@SuppressWarnings("serial")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("GENERIC")
public class ArtData extends AbstractDomainEO  implements DomainObject {

更新#2 .... 所以我决定采用另一个没有继承的简单类,只是为了让版本工作,但也没有运气。我的新课:

@Entity
@Table(name = "CAR")
public class CarEO implements Car {

private Date createdDate;

@Version 
private Integer version;

public Integer getVersion() {
    return version;
}
public void setVersion(Integer version) {
    this.version = version;
}

public void setCreatedDate(Date createdDate) {
    this.createdDate = createdDate;
}

@Temporal(TemporalType.TIMESTAMP)
public Date getCreatedDate() { 
    return this.createdDate; 
}


@Id
@GeneratedValue
@Column(name = "CAR_ID")
private Integer id;
private String name;
public CarEO() {
    super();
}

@Override
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CAR_ID_SEQ")   
public Integer getId() {
    return this.id;
}

@Override
public void setId(Integer id) {
    this.id = id;
}

@Override
public String getName() {
    return this.name;
}

@Override
public void setName(String name) {
    this.name = name;
}

但是我仍然为版本和 createDate 得到空值(但这是一个单独的问题)

CarEO[createdDate=,version=0,id=3,name=bmw,comments=[]]

这是我尝试创建时的输出,为此 EO 对象选择删除:

[org.hibernate.cache.StandardQueryCache]; 使用默认值。[DEBUG] org.hibernate.SQL:111 - 插入 CAR(CAR_ID、createdDate、name、version)值(null、?、?、?)

Hibernate:插入 CAR(CAR_ID、createdDate、name、version)值(null、?、?、?)

[INFO] CarDaoJPASpringTest:64 - * ** * ** * ** * **** [DEBUG] org.hibernate.SQL:111 - 选择 careo0_.CAR_ID 作为 CAR1_1_,careo0_.createdDate 作为 createdD2_1_,careo0_.name 作为 name1_ , careo0_.version 作为版本 1_ 来自 CAR careo0_

休眠:从CAR careo0_中选择careo0_.CAR_ID作为CAR1_1_,careo0_.createdDate作为createdD2_1_,careo0_.name作为name1_,careo0_.version作为version1_

CarEO[createdDate=,version=0,id=3,name=bmw,comments=[]] [DEBUG] org.hibernate.SQL:111 - 从 CAR 中删除 CAR_ID=? 和版本=?休眠:从 CAR 中删除 CAR_ID=? 和版本=?

4

1 回答 1

0

用起来很想念MappedSuperClassMapped superclasses不可查询,也不能用于EntityManagerorQuery操作。 EntityManager将在执行 DB 操作Queryversion执行操作。现在,这是一个问题,因为您在. 为了避免这个问题,你必须放入或类。EntityManagerArtDataversionMappedSuperClassversionEntityPOJO

示例 1

@MappedSuperclass 
public abstract  class AbstractDomainEO {
    private Date createdDate;
}

@SuppressWarnings("serial")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("GENERIC")
public class ArtData extends AbstractDomainEO  implements DomainObject {

    @Version 
    private Integer version;

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
}

示例 2

@Entity
public abstract class Parent {
    @Version 
    private Integer version;

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
}

@MappedSuperclass 
public class AbstractDomainEO extends Parent {
    private Date createdDate;
}

@SuppressWarnings("serial")
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("GENERIC")
public class ArtData extends AbstractDomainEO  implements DomainObject {

}

参考资料MappedSuperclass。_

于 2012-11-10T05:21:45.243 回答