I've got a class named DepenseBeneficiaire.java, which primary key is composed of attributes from the class DepenseBeneficiaireId.java. One of DepenseBeneficiaireId attributes is an enumeration, to determine which type of DepenseBeneficiaire we are dealing with.
These classes are saved in my database without any problem, but I'm having some trouble reloading them: obviously, the column dpeense_id cannot be matched back with the Java enum.
DepenseBeneficiaire.java
Code:
@Entity
@Table(name="dep_beneficiaire")
public class DepenseBeneficiaire implements Serializable
{
private static final long serialVersionUID = 3872180192633198521L;
public static final String FIELD_IDBENEFICIAIRE = "identifier";
public static final String FIELD_BENEFICIAIRE = "beneficiaire";
private DepenseBeneficiaireId identifier;
@EmbeddedId
@AssociationOverrides
({
@AssociationOverride
(
name="beneficiaire",
joinColumns=
{
@JoinColumn(name="dossier_id", referencedColumnName="dossier_id"),
@JoinColumn(name="action_id", referencedColumnName="action_id"),
@JoinColumn(name="beneficiaire_id", referencedColumnName="id")
}
),
@AssociationOverride
(
name="depense_id",
joinColumns=
{
@JoinColumn(name="depense_id", referencedColumnName="depense_id")
}
)
})
public DepenseBeneficiaireId getIdentifier() {
return identifier;
}
public void setIdentifier(DepenseBeneficiaireId identifier) {
this.identifier = identifier;
}
@ManyToOne
@JoinColumns
({
@JoinColumn(name="dossier_id", referencedColumnName="dossier_id", insertable=false, updatable=false),
@JoinColumn(name="action_id", referencedColumnName="action_id", insertable=false, updatable=false),
@JoinColumn(name="beneficiaire_id", referencedColumnName="id", insertable=false, updatable=false)
})
public Beneficiaire getBeneficiaire()
{
return identifier.getBeneficiaire();
}
public void setBeneficiaire(Beneficiaire beneficiaire)
{
identifier.setBeneficiaire(beneficiaire);
if(beneficiaire.getAdherent_Id() != null) setBeneficiaire_type("A");
if(beneficiaire.getFournisseur_Id() != null) setBeneficiaire_type("F");
if(beneficiaire.getOrganisme_Id() != null) setBeneficiaire_type("O");
if(beneficiaire.getStagiaire_Id() != null) setBeneficiaire_type("S");
}
@Column(name="depense_id", length=3, insertable=false, updatable=false)
public NatureDepense getDepense_id() {
return identifier.getDepense_id();
}
public void setDepense_id(NatureDepense depense_id) {
this.identifier.setDepense_id(depense_id);
}
public DepenseBeneficiaire()
{
identifier = new DepenseBeneficiaireId();
}
public DepenseBeneficiaire(NatureDepense natureDepense)
{
this();
setDepense_id(natureDepense);
}
@Override
public boolean equals(Object obj)
{
if(!(obj instanceof DepenseBeneficiaire))
return false;
DepenseBeneficiaire depenseBeneficiaire = (DepenseBeneficiaire)obj;
return
new EqualsBuilder()
.append(depenseBeneficiaire.getIdentifier(), getIdentifier())
.isEquals();
}
@Override
public int hashCode()
{
return
new HashCodeBuilder()
.append(getIdentifier())
.toHashCode();
}
}
et DepenseBeneficiaireId.java
Code:
@Embeddable
public class DepenseBeneficiaireId implements Serializable
{
public enum NatureDepense
{
REMUNERATION ("01", "REMUNERATION"),
FORMATION ("03", "FORMATION"),
REPAS ("07", "REPAS"),
HEBERGEMENTREPAS ("08", "HEBERGEMENT ET REPAS"),
TRANSPORT ("09", "TRANSPORT"),
DIVERS ("10", "DIVERS"),
REMUNERATIONHTT ("16", "REMUNERATION HTT"),
ALLOCATIONFORMATION ("17", "ALLOCATION FORMATION"),
LOCATIONSALLE ("18", "LOCATION SALLE"),
LOCATIONMATERIEL ("19", "LOCATION MATERIEL");
private String identifier;
private String libelle;
public String getIdentifier() {
return identifier;
}
public String getLibelle() {
return libelle;
}
private NatureDepense(String identifier, String libelle)
{
this.identifier = identifier;
this.libelle = libelle;
}
public static NatureDepense value(String identifier)
{
NatureDepense result = null;
for(NatureDepense natureDepense: values())
{
if(natureDepense.getIdentifier().equals(identifier))
{
result = natureDepense;
break;
}
}
return result;
}
}
private static final long serialVersionUID = -4762088849677965696L;
public static final String FIELD_BENEFICIAIRE = "beneficiaire";
private Beneficiaire beneficiaire;
private NatureDepense depense_id;
@Type
(
type="fr.site.hibernate.utils.GenericEnumUserType",
parameters=
{
@Parameter(name="enumClassName", value="fr.site.hibernate.classes.DepenseBeneficiaireId$NatureDepense"),
@Parameter(name="valueOfMethod", value="value")
}
)
public NatureDepense getDepense_id() {
return depense_id;
}
public void setDepense_id(NatureDepense depense_id) {
this.depense_id = depense_id;
}
@ManyToOne
public Beneficiaire getBeneficiaire() {
return beneficiaire;
}
public void setBeneficiaire(Beneficiaire beneficiaire) {
this.beneficiaire = beneficiaire;
}
@Override
public boolean equals(Object obj)
{
if(!(obj instanceof DepenseBeneficiaireId))
return false;
DepenseBeneficiaireId id = (DepenseBeneficiaireId)obj;
return
new EqualsBuilder()
.append(id.getBeneficiaire().getId(), getBeneficiaire().getId())
.append(id.getDepense_id().getIdentifier(), getDepense_id().getIdentifier())
.isEquals();
}
@Override
public int hashCode()
{
return
new HashCodeBuilder()
.append(getBeneficiaire().getId())
.append(getDepense_id().getIdentifier())
.toHashCode();
}
}
I get the following exception:
Code:
java.lang.IllegalArgumentException: Unknown name value for enum class fr.site.hibernate.classes.DepenseBeneficiaireId$NatureDepense: 16
[...]
Caused by: java.lang.IllegalArgumentException: No enum const class fr.site.hibernate.classes.DepenseBeneficiaireId$NatureDepense.16
meaning that Hibernate is trying to load the constant
NatureDepense.16 which doesn't exist, but shouldn't function
public static NatureDepense value(String identifier) be handling this matter? I ust know that Hibernate uses this function while reloading, and matches the given String with the corresponding NatureDepense.
I don't know if it's because my enumeration identifier is a String, or if I'm missing an annotation (I've tried to use @Enumerated(EnumType.STRING), but this doesn't seem to work better), or if I have to override something in my enum: any idea?