-->
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.  [ 8 posts ] 
Author Message
 Post subject: How to make entities read-only after database insertion
PostPosted: Fri Dec 14, 2007 4:21 pm 
Beginner
Beginner

Joined: Thu Sep 16, 2004 8:14 pm
Posts: 27
Hibernate version: 3.2.5 GA

I have a number of entities that need to be capable of adding new rows of data to the database, but after insertion those rows must never be updated again. Is there a way to enforce this with annotations or other Hibernate configuration?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 8:01 am 
Newbie

Joined: Thu Dec 13, 2007 2:52 pm
Posts: 3
Location: Prague
Maybe we can understand the question more clearly if you tell:

- What are those entities that need to be capable of adding rows? Aren't these rows correspond to other entities? What are those rows about? Why they should not be updated?

- Did you try enforcing a business rule in your domain model which would be totally independent of the underlying persistence mechanism?

_________________
Bahadir KONU


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 15, 2007 8:25 am 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
I don't think you can label an Entity as insert only. However you can annotate all your columns as updatable=false, e.g.

@Column(
name = "...",
updatable = false
)
public ... get...(...) { ... }

This should give your nearly the effect you wanted.

Note that the above refers to JPA annotations not Hibernate annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 7:53 pm 
Beginner
Beginner

Joined: Thu Sep 16, 2004 8:14 pm
Posts: 27
Annotating the getter with that annotation does prevent the entity from being updated, but it doesn't appear to cause any kind of exception to be thrown. I'd like an exception to be thrown so that the developer is aware that they are attempting to do something that they should not. Right now it would be easy to assume that the update to the entity had succeeded without checking the database.
Is there anything I can do to get an exception thrown?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 8:56 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
What about adding a @PreUpdate (and @PreRemove) callback method to your entity which throws an unchecked exception like:

Code:
@PreUpdate
private preUpdate() {
     throw new IllegalStateException("You are being very naughty!")
}
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 9:11 pm 
Beginner
Beginner

Joined: Thu Sep 16, 2004 8:14 pm
Posts: 27
I tried adding @PreRemove and a

public void preRemove() method as suggested and nothing happens when I call a setter on the entity. Is there something else I need to do?
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 9:36 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
The @PreUpdate and @PreRemove are only called just before the persistence runtime issues the SQL update or delete statement. They are not called when setters are invoked.

If all you want to do is stopping clients of your entity to call the setters then just make them private.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 17, 2007 9:49 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Sorry, you may say then users cannot create new entities as they can't populate them.

Solution 1) Give them a constructor which has arguments for all fields. This is part of the standard OO solution if you want to make an object immutable.

Solution 2) Use a @PostLoad and @PostPersist callback which sets a local boolean field and in all your setters test that field. If its true throw an exception.

Code:

private boolean locked = false;

@PostLoad
@PostPersist
private void postLoad() { locked = true; }

public void set...(...) { if (locked) throw new ...; ...}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.