S
S
ScRsa2016-07-11 06:25:28
Java
ScRsa, 2016-07-11 06:25:28

How to properly make a ReadOnly field with a condition?

Prompt as it is correct to make ReadOnly a field. The meaning is this:
There is a document. A document has a document type. When creating a document, you can specify any type, but you cannot change the document type during subsequent editing. Or continuation - only the administrator can change the document type.
UPD I
use Spring, Spring Data (hibernate)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
protven, 2016-07-11
@protven

Not in the standard library, maybe there are some ready-made solutions in third-party ones, but I have not seen it.
"Hands" can be done corny like this.
In the setter for the "Class type" field, check if it is null, then replace it. If not (that is, it was already set earlier), then throw an exception / return an error / process it somehow there.
If it is assumed that this section of code can be entered from different threads, then you need to take care of potential race conditions. For example, it's trivial to make a setter synchronized.
As for the fact that only the administrator can change the document type. Well, enter some kind of role system in your application and do an additional check. Since you haven't written the technology stack you're using, I can't write anything specific.

S
sirs, 2016-07-11
@sirs

If you use hibernate, then try adding a property to the column in the table (it is also a field in the bin)

property name="docType" insert="true" update="false"
link is for when you don't want to change after creation.
If you want only the administrator to be able to change, you need to look towards spring security

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question