Hibernate不持久枚举(如String)

时间:2014-02-27 21:00:36

标签: java hibernate jpa enums

我正在尝试做一些简单的事情,将Hibernate枚举值保存为字符串。我的映射看起来像这样:

@Entity
@Table(name = "CONTRACTOR")
public class Contractor
{
    //some unreleated columns...

    @OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, mappedBy = "contractor")
    private List<ContractorGeoAvailability> geoAvailabilities;

    //getters, setters
}

有anum的实体:

@Entity
@Table(name = "CONTRACTOR_GEO_AVAILABILITY")
public class ContractorGeoAvailability
{
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @JoinColumn(name = "CONTRACTOR_ID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Contractor contractor;

    @Column(name = "TYPE")
    @Enumerated(EnumType.STRING)
    private GeoAvailibilityType geoAvailibilityType;

    @Column(name = "VALUE")
    private String value;

    ....
}

并且枚举:

public enum GeoAvailibilityType
{
    DISTRICT,
    CITY,
    VOIVODESHIP,
    COUNTRY
}

从提交的表单中我收到正确的值(HTTP POST):

------WebKitFormBoundary1huaDyI1dvn9KGnI
Content-Disposition: form-data; name="geoAvailibilities[0].type"

CITY
------WebKitFormBoundary1huaDyI1dvn9KGnI
Content-Disposition: form-data; name="geoAvailibilities[0].value"

test
------WebKitFormBoundary1huaDyI1dvn9KGnI
Content-Disposition: form-data; name="geoAvailibilities[1].type"

VOIVODESHIP
------WebKitFormBoundary1huaDyI1dvn9KGnI
Content-Disposition: form-data; name="geoAvailibilities[1].value"

偶数值与DTO对象正确绑定:

ContractorRegisterForm{.... geoAvailibilities=[GeoAvailibility{type=CITY, value='test'}, GeoAvailibility{type=VOIVODESHIP, value='test321'}], profile=null}

但是在某种程度上持续存在enum的价值会丢失:

21:55:59,123 INFO  [stdout] (http--127.0.0.1-8080-3) Hibernate: 
21:55:59,123 INFO  [stdout] (http--127.0.0.1-8080-3)     insert 
21:55:59,123 INFO  [stdout] (http--127.0.0.1-8080-3)     into
21:55:59,123 INFO  [stdout] (http--127.0.0.1-8080-3)         CONTRACTOR_GEO_AVAILABILITY
21:55:59,123 INFO  [stdout] (http--127.0.0.1-8080-3)         (CONTRACTOR_ID, TYPE, VALUE, ID) 
21:55:59,124 INFO  [stdout] (http--127.0.0.1-8080-3)     values
21:55:59,124 INFO  [stdout] (http--127.0.0.1-8080-3)         (?, ?, ?, ?)
21:55:59,124 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http--127.0.0.1-8080-3) binding parameter [1] as [BIGINT] - 19
21:55:59,124 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http--127.0.0.1-8080-3) binding parameter [3] as [VARCHAR] - test
21:55:59,124 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http--127.0.0.1-8080-3) binding parameter [4] as [BIGINT] - 20
21:55:59,125 INFO  [stdout] (http--127.0.0.1-8080-3) Hibernate: 
21:55:59,125 INFO  [stdout] (http--127.0.0.1-8080-3)     insert 
21:55:59,125 INFO  [stdout] (http--127.0.0.1-8080-3)     into
21:55:59,125 INFO  [stdout] (http--127.0.0.1-8080-3)         CONTRACTOR_GEO_AVAILABILITY
21:55:59,125 INFO  [stdout] (http--127.0.0.1-8080-3)         (CONTRACTOR_ID, TYPE, VALUE, ID) 
21:55:59,126 INFO  [stdout] (http--127.0.0.1-8080-3)     values
21:55:59,126 INFO  [stdout] (http--127.0.0.1-8080-3)         (?, ?, ?, ?)
21:55:59,126 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http--127.0.0.1-8080-3) binding parameter [1] as [BIGINT] - 19
21:55:59,126 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http--127.0.0.1-8080-3) binding parameter [3] as [VARCHAR] - test321
21:55:59,126 TRACE [org.hibernate.type.descriptor.sql.BasicBinder] (http--127.0.0.1-8080-3) binding parameter [4] as [BIGINT] - 21

看起来Hibernate完全忽略了枚举值,因为它持续存在第1,第3和第4个参数而不是第2个参数(参见binding parameter [.])。有人可以帮我吗?

0 个答案:

没有答案