Hi all,
Here's the situation - I would like my beans not to permit their ID to be altered. So I have given them a get method, but no set method for the ID. I would also like Hibernate to access properties via their accessors, rather than over-ride security.
In order to achieve this I have included the ID in the sole constructor, and written an interceptor that instantiates the bean via that constructor. I have also specified that the Generator type is assigned.
At that point I found that Hibernate wouldn't parse the .hbm.xml file, because it couldn't find a setter for the ID property. So I added access="field" to the id tag in the .hbm.xml. Voila, all works fine, and if I set a watch on the id field I find that actually Hibernate never does try and set the id property directly. So I'm happy. This is how it looks:
Code:
<hibernate-mapping>
<class name="Address">
<id name="addressId" type="string" access="field">
<column name="address_id" length="40" />
<generator class="assigned"/>
</id>
<property name="postcode" type="string">
<column name="postcode" length="10" />
</property>
</class>
</hibernate-mapping>
public class Address implements Serializable {
private String addressId;
private String postcode;
public AddressImpl(String addressId) {
this.addressId = addressId;
}
public Serializable getAddressId() {
return addressId;
}
public String getPostcode() {
return this.postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
}
But now I want to do it using annotations. So I make my class look like this:
Code:
@Entity(access = AccessType.PROPERTY)
public class Address implements Serializable {
private String addressId;
private String postcode;
public AddressImpl(String addressId) {
this.addressId = addressId;
}
@Id(generate = GeneratorType.NONE)
public Serializable getAddressId() {
return addressId;
}
public String getPostcode() {
return this.postcode;
}
public void setPostcode(String postcode) {
this.postcode = postcode;
}
}
And once again, Hibernate complains when parsing the annotations because there is no setter for addressId. But this time there's no way for me to over-ride the access type for the Id field to FIELD.
Is there any way to either tell Hibernate not to complain about the lack of setters? Or to over-ride the access settings for individual properties when using Annotations?
Thanks very much,
Rob