#NotNull type annotation - Solrj - java

I have solr schema where almost every field is required=true.
I am using following POJO to persist data in solr
#Field(value = "id")
private String id;
#Field(value = "entity_id")
private Long entityId;
...
Now before sending it to solr, I am currently checking explicitly each field for null. Is there any annotation like #NotNull or #Field attribute like required=true so that I need not to check them ?

Unfortunately, I do not believe that SolrJ has this capability. (Source - I looked at the source code. Field does not have this defined, nor are there other beans that might be useful.)

Related

How to enforce unique field with MongoDB in Spring

I have a pojo with two fields that need to be unique, the id and the email. So I added the #Indexed(unique = true) annotation to the necessary fields like so
public class User {
#Id
#Indexed(unique = true)
private String id;
#Indexed(unique = true)
private String email;
private int money;
I then tested it out and it was not enforced. So I googled about and I found a previous answer here - Spring Data: Unique field in MongoDB document and subsequently deleted the collection, added spring.data.mongodb.auto-index-creation=true to my application.properties file and tried again.
However, the unique field still isn't enforced! I see there is another answer using ensureIndex() but it also has a great comment that was never answered- Why do we need to use the annotation if all the work is done on mongoTemplate?
So since the question is old enough that apparently the only working answer is depreciated (the new way is using createIndex()), I thought it was time for a new version. Is it possible to require a column in a mongo collection to be unique from Spring Boot?

Spring JPA + Hibernate Search : How update the Search Index(Lucene) only?

I use Spring JPA + Hibernate Search to implement persistant and search in my application.
I have models like this
public class FeatureMeta {
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#Column(unique=true)
private String uri;
#Column
#Field
private String name;
#Field
#Column
private String businessDesc;
#Field
#Column
private String logicalDesc;
.
.
#Field
#Column(insertable=false,updatable=false)
private Long totalDownloads;
.
.
}
To give the idea about this class, "FeatureMeta" maintains meta-data information which updates very rarely.
However the field "totalDownloads" is constantly changing whenever user download information about this "feature".
Basically "totalDownloads" is not part of the meta-data but I had to put this field in the model because I need to show the "totalDownloads" in the search result of "feature search".
I use same JPA Repository which updates both MySQL and Lucene index.
My question is ; Is it possible to only update the "totalDownloads" in the Lucene Index but not the entity in MySQL whenever change is done to the "totalDownloads" field ?
You'll have to use the #Transient annotation to mark that you don't want this attribute part of your database model.
#Field
#Transient
private Long totalDownloads;
Making the field transient also means it won't be loaded from the database (completely ignored by Hibernate ORM, but not by Hibernate Search); if that's not what you intended you could add an additional field: map one to Hibernate ORM and the other indexed with Hibernate Search and annotated with #Transient. In this case you'll have to make the setter update both fields.
You will likely need to change this configuration property too:
hibernate.search.enable_dirty_check = false
as Hibernate Search will otherwise not generate any change in the Lucene index, in case the entity has no other changes.

eclipselink struct fields order

I am working on a EclipseLink application, which uses Oracle Objects as IN and OUT parameters (while invoking stored procedure). As you know we have #Struct annotations available in Eclipselink for representing Oracle Object, I used it and it is working perfectly. But, looks like order of the fields declared in Struct annotated class matters a lot to map to correct field in oracle object. This causes maintenance issues and very difficult to code when object's properties are more. Is there a way in Eclipselink to say map Structure fields based on name and not with order.
Ex: Below is my Struct class. If by chance I declare variables in different order from fields list, wrong/incorrect mappings will happen while fetching records from stored proc. Its always mapping values to fields from top to bottom. #Column name annotation is not able to solve this issue.
#Struct(name = "REC_OBJECT",
fields = {"TRANS_ID", "PROJECT_ID", "LANGUAGE_CODE", "DESCRIPTION"})
#Embeddable
public class Master {
#Column(name = "PROJECT_ID")
private String projectId;
#JsonIgnore
#Column(name = "TRANS_ID")
private String transactionId;
#Column(name = "LANGUAGE_CODE")
private String languageCode;
#Column(name = "DESCRIPTION")
private String description;
}
Please suggest solution for this. Thank you.

Spring Hibernate Bean Validation #valid

I know it is not new question in this forum but I am very confused what should i do.
Problem: I am developing one application with spring mvc + hibernate. For server side validation I am using #valid annotation in controller and #null and #notNull annotation in my bean.
e.g
public class User implements Serializable {
private static final long serialVersionUID = 2158419746939747203L;
#Id
#Column(name="USER_ID")
#GeneratedValue(strategy=GenerationType.IDENTITY)
private long userId;
#Column(name="USERNAME", unique = true)
#NotEmpty #NotNull #Size(min=6, max=20)
private String username;
#Column(name="PASSWORD")
#NotEmpty #NotNull #Size(min=6, max=20)
private String password;
This validation is happening fine and data is also saving in DB.
But I want to validate unique constraint,referential integrity and other constraint using annotation without any validator class.
Is it possible? if no, then what is best and easiest way to to do it(less coding)? I will appreciate if framework will do it for me.
Saurabh,
For unique constraint in table,
#Id
You will be able to enforce referential integrity via hibernate annotations as well
eg.
#OneToOne(mappedBy = "foo")
Here is an example post
Referential integrity with One to One using hibernate
Here is a very detailed tutorial also exploring the same:
http://www.journaldev.com/2882/hibernate-tutorial-for-beginners-using-xml-annotations-and-property-configurations
You could write a "CustomUniqueConstraintValidator" kinda like mentioned in
http://www.journaldev.com/2668/spring-mvc-form-validation-example-using-annotation-and-custom-validator-implementation
You can also pass in paramters from the annotation to the custom validator.
eg.
#CustomValidDate("columnName")
To make a generic class that applies for any field /column
1. YOu can write a generic custom validator
2. use annotaiton parameters (on each class attribute) to pass in the table name and column name.
3. Then in the validator you can use the table name, column name to apply your validation logic (unique etc).
Thanks,
Paul

JPA: how do I persist a String into a database field, type MYSQL Text

The requirement is that the user can write an article, therefore I choose type Text for the content field inside mysql database. How can I convert Java String into MySQL Text
Here you go Jim Tough
#Entity
public class Article implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long userId;
private String title;
private String content;
private Integer vote;
//Constructors, setters, getters, equals and hashcode
}
In my MYSQL database, content is type Text. I was hoping that there would be something like this java.sql.Text, since java.sql.Blob is an actual type, but sadly, that does not exist
Since you're using JPA, use the Lob annotation (and optionally the Column annotation). Here is what the JPA specification says about it:
9.1.19 Lob Annotation
A Lob annotation specifies that a
persistent property or field should be
persisted as a large object to a
database-supported large object type.
Portable applications should use the
Lob annotation when mapping to a
database Lob type. The Lob annotation
may be used in conjunction with the
Basic annotation. A Lob may be
either a binary or character type. The
Lob type is inferred from the type of
the persistent field or property, and
except for string and character-based
types defaults to Blob.
So declare something like this:
#Lob
#Column(name="CONTENT", length=512)
private String content;
References
JPA 1.0 specification:
Section 9.1.19 "Lob Annotation"
With #Lob I always end up with a LONGTEXTin MySQL.
To get TEXT I declare it that way (JPA 2.0):
#Column(columnDefinition = "TEXT")
private String text
Find this better, because I can directly choose which Text-Type the column will have in database.
For columnDefinition it is also good to read this.
EDIT: Please pay attention to Adam Siemions comment and check the database engine you are using, before applying columnDefinition = "TEXT".
for mysql 'text':
#Column(columnDefinition = "TEXT")
private String description;
for mysql 'longtext':
#Lob
private String description;

Categories