-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Over-riding access settings?
PostPosted: Fri Oct 14, 2005 12:07 pm 
Newbie

Joined: Fri Oct 14, 2005 11:40 am
Posts: 7
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 7:50 am 
Newbie

Joined: Fri Oct 14, 2005 11:40 am
Posts: 7
OK, looks like I'm not getting an answer to this one! I've added a private setter, now Hibernate is happy.

Is there some info on security managers around? I'm wondering how easy they are to configure; if you were deploying code somewhere where it needed a security manager, can you tell it to let Hibernate muck around with private methods but not other classes?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.