Hi,
Let's say I have an entity with automatic id generation. In this case, I don't want to have a setter, just a getter. This works fine when I put the @Id annotation on an attribute :
Code:
@Entity
public class Country implements Serializable {
@Id @GeneratedValue
private Long id;
private String code;
private String name;
public Long getId() {
return id;
}
...
}
Now, when I move the @Id annotation to the getter, I get an exception saying that I need a setter :
Code:
@Entity
public class LongTermAssetRating implements Serializable {
private Long id;
private String code;
private String name;
@Id @GeneratedValue
public Long getId() {
return id;
}
// Needs a setter to work
public void setId(Long id) {
this.id = id;
}
...
}
Code:
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property id in class fr.bdf.staticData.domain.StaticDataSecurity
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:240)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:233)
at org.hibernate.mapping.Property.getSetter(Property.java:299)
Why is that ? I thought annotations could work seemlessly on attributes or getters. In this case, it forces me to have a setter but I don't want it. When you look at the Hibernate code (org.hibernate.property.BasicPropertyAccessor.createSetter(Class theClass, String propertyName)) it clearly throws an exception if the setter doesn't exist.
Any idea on that ?
Thanks,
Antonio