adding a constraint to spring entity set field value to one of possible predefined values - spring

good day everyone,
i'm currently working on a spring application for school project, it's about a form generator. anyway i have a question entity which has a field called type, i want that field to be one of 3 different values (radio which is the default, checkbox, or input) but i didn't know how to do this with hibernate annotations or even with the spring constraints annotations so i was hoping someone here has an idea on how to do this.
thanks for your time everyone and hope you have a nice day.

Related

when I try to add a second field set to an entity it doesn't show in the screens

I have six entities and I want to create two field sets from one to two others. One field set is ONE_TO_MANY and I used field set in the ONE side to build the relation and the other is a MANY_TO_MANY and I tried the simplest example from Northwind and that didn't work, and I tried adding --joinTable, --joinColumns, etc. and that didn't work either. Only the ONE_TO_MANY works and only one of those.
Is there a good example with Roo 2.0.0.RC1 that works? or a manual work around?
Remember that Spring Roo just generates and maintains the code that he knows. In this case, you are trying to create a really specific structure, so I recommend you to create it manually. Spring Roo doesnt't have any problem with custom code.
You could check the following repository where exists a lot of examples about relation management managed by Spring Roo or manually.
https://github.com/DISID/disid-proofs
Hope it helps,

Can hibernate validator be used to validate a field depending on a field value from another class

I have a scenario where I need to validate some fields of a class depending on the value of a field from another class.
For ex: Street, City, Postal Code of Address class should not be null if the Employee has registered for a Group Retirement Plan.
Can this scenario be handled using Custom Validator of Hibernate Validator? Is this a good candidate for Hibernate Validator ? If so, could anyone provide me with some examples.
I have already gone through cross field validation examples like:
Cross field validation with Hibernate Validator (JSR 303)
If I have a boolean indicator to indicate whether a user is registered for group retirement plan in Address domain class, I know I can come up with Custom Validator to verify the fields. But, I want to avoid adding indicator to Address class. As well, Address is not a field in Employee class to do nested validation.
The Frameworks I am using are Spring MVC and Mybatis for Data access. The validation is happening at Spring Rest API call with #Valid annotation.
I guess a custom class level constraint might work, but it depends on your actual domain classes. It is currently not clear how for example Employee, Group Retirement Plan and these other entities are connected.
Whether Hibernate Validator is a good fit is whether you have other validations as well or which other frameworks are involved. For example, if you want validation to take place at JPA live cycle events, then Bean Validation is a good fit, since it integrates per specification with JPA. If you really only have this one validation, writing some custom code might be all you need.

Setting default values in Java Beans / Data transfer Objects

We use Data Transfer Objects (DTO) in our code. When constructing them, it is inevitable that some of their fields will have null values as some of those fields are null in the database. We were told that all null values must be default to "NA".
I know we can implements this the "hard way" by putting logic in the get methods of the DTOs. Question is, is there a better way to do this? I tried using #Value annotation of Spring to set its default values but this does not work.
Can anyone help? Thank you.
if you dont have a front end, you have to do it at the get method. (add in if statement for null?)

How to display entity framework object as model with validation?

I have a populated object from using the entity framework. Let's call it Order. The order has different properties such as Id, OrderDate, BillingAddress and so on. I need to let users update this data.
What's the best way to display this data in a form, while enforcing data annotations such as [Required]? I see MetadataType mentioned a lot, but I haven't seen how I can connect the dots with displaying the data as well.
One approach that I could take, but I'd like to avoid because of redundancy, is creating my own model object that has nearly identical properties. Then I would just need to basically just copy entity framework object A to new object B, where B has all my lovely data annotations. It just seems like there might be a better way.
Could anyone provide me with an example of a good way to accomplish this?
The "better way" is a big reason EF Code First is great. Otherwise, the only other way to do what you need is to do a mapping.

Creating "lookup" values in Core Data

I have a Core Data entity which needs a gender property. I just need one of two, the standard male or female values. What's the best practice for this in Core Data?
In the .NET world with databases I would've created a Gender table, with foreign key in the child table. I'm still getting my head around Core Data right now - any suggestions would be greatly appreciated.
Cheers,
Dany.
UPDATE
Based on the comments here I have added an extra NSString property in my Core Data entity called gender. Its getter and setter manipulate the isMale property value. The UI is bound to gender - works a treat so far! Thanks for the help everyone.
I would not do a look up as Core Data is not a database but rather an object hierarchy that happens to persist to a database.
Instead I would have a boolean called male with a getter accessor -isMale since there is no risk of there ever being a third.
update
Despite the commentary, how you interact on the UI is completely separate from how you store the data. How you display is up to your UI design. The code in your controller will handle the translation between the boolean state and the UI display.
You can have a checkbox, radio buttons, drop down list, et. al.; doesn't matter. Just translate what the user interacts with in your controller.

Resources