'Hibernate not persisting enums (as String)
I'm trying to do simple thing, save with Hibernate enum value as string. My mapping looks like this:
@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
}
Entity with 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;
....
}
And enum:
public enum GeoAvailibilityType
{
DISTRICT,
CITY,
VOIVODESHIP,
COUNTRY
}
From submitted form I receive correct values (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"
Even value is correctly binded with DTO object:
ContractorRegisterForm{.... geoAvailibilities=[GeoAvailibility{type=CITY, value='test'}, GeoAvailibility{type=VOIVODESHIP, value='test321'}], profile=null}
but somehow during persisting enum's value is lost:
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
It looks like the enum value is completly ignored by Hibernate, as it persists 1st, 3rd and 4th parameter not 2nd one (see binding parameter [.]
). Could someone help me with that?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|