Adding a new field with default value in a collection in Spring Data MongoDB - spring

I am adding a new boolean field in a collection by adding an attribute to a Java Class entity that is being used in the MongoRepository interface. However, the existing documents' new field is being initialised as null in the database. I want the new field's default value in existing documents to be set to false. How can this be done in Spring Data MongoDB?

Let say your version 1 Java entity was
#Document
Public Class Person {
#Id
Private String id;
private String firstName;
private String lastName;
.........
.........
And later you have introduced
#Document
Public Class Person {
#Id
Private String id;
private String firstName;
private String lastName;
private Boolean isAlive; //New Boolean attribute
.........
.........
In java the default value of an instance object is null and so a document in mongoDB with no value for isAlive will be defaulted to null. If you need the default value to be false you could do this
private Boolean isAlive = Boolean.FALSE;

Related

I have a field in database which I do not want to map in my spring java model while making a get call

I have a database table in which there is a field which I do not want to map to my model class while making a get call. Is there any annotation to handle this use case?
When persisting Java objects into database records using an Object-Relational Mapping (ORM) framework, we can ignore fields by adding the #Transient annotation to those fields.
#Entity
#Table(name = "Users")
public class User {
#Id
private Integer id;
private String email;
private String password;
#Transient
private Date loginTime;
// getters and setters
}

Spring Data for Couchbase - Metadata Information

I am new using Spring Data for Couchbase, I defined this object
public class Building {
#NotNull
#Id
private String id;
#NotNull
#Field
private String name;
#NotNull
#Field
private String companyId;
}
but I am not sure if the id defined in the object will be the same of the Couchbase Metadata Information and how to define the format of it
.cas
.expiration
.flags
.id
.type
You don't need to worry about the meta attributes, they are automatically created by couchbase. Just the #Id is mandatory.

Couchbase spring data jpa to generate composite primary key

Using Couchbase with Spring Data and JPA,
In my entity class How to create a Composite Key using two fields.
currently in Person class id is the primary key #Id
where I want combination of id and name will be primary key.
#Document
public class Person {
#Id
private String id;
#Field
private String name;
#Field
private String city;
I think you should be able to achieve what you want using the following -
#Document
public class Person {
#Id
#GeneratedValue()
String key;
#IdPrefix
String id;
#Field
#IdAttribute
String name;
#Field
private String city;
}
Use #IdSuffix if you want the value in id to be after the name . When constructing the above object don't assign a value to key variable yourself as the value doesn't get autogenerated if you provide one of your own as per docs - https://docs.spring.io/spring-data/couchbase/docs/current/reference/html/#couchbase.autokeygeneration.configuration .

How to use Spring Data JPA findAll method in Spring Boot with custom parameter?

I have a Entity class
#Entity
public class SampleEntity {
#Id
#Column(name = "ID")
private Long id;
#NotNull
#Column(length = 2000)
private String name;
#NotNull
#Column(length = 2000)
private String type;
#NotNull
#Column(length = 2000)
private String something;
// getters and setters
}
What I need is to findAll by the custom column of my entity type
how I can achieve this in Spring boot and JpaRepository
Follow https://docs.spring.io/spring-data/jpa/docs/1.5.0.RC1/reference/html/jpa.repositories.html which will list all possible method to find out records from DB.
For your problem , use List<SampleEntity> findByType(String type) in your repository. This method will return all SampleEntity by type values passed in query.
Besides the findByType(String type) you can use the Spring Data repository syntax to fine tune your queries, using the same parameter String type.
Some examples:
findByTypeIgnoreCase: will return all that match the given type string ignoring case differences between that string and the SampleEntity type field.
findFirstByType: will return the first element with the given type if there is any.
findFirst100ByType: will return the first 100 elements.
findByTypeOrderByIdAsc: will return all SampleEntity with the given type, in ascending order by their id field.
countByType: will return the number of SampleEntity with the given type.
removeByType: will delete all the SampleEntity with that type.

How to search nested object by using Spring Data Solr?

I have two such Java object:
public class PSubject
{
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("name")
private String name;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("type")
private String type;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("uri")
private String uri;
#OneToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
#IndexedEmbedded
#org.apache.solr.client.solrj.beans.Field("attributes")
private Set<PAttribute> attributes = new HashSet<PAttribute>();
.....
}
#Entity
#Indexed
#Table(name="PAttribute")
#Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class PAttribute extends PEntity
{
private static final long serialVersionUID = 1L;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)
#org.apache.solr.client.solrj.beans.Field("attr_name")
private String name;
#Column
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.YES)
#org.apache.solr.client.solrj.beans.Field("attr_value")
private String value;
.....
}
And my Spring Data Solr query interface:
public interface DerivedSubjectRepository extends SolrCrudRepository<PSubject, String> {
Page<PSubject> findByName(String name, Pageable page);
List<PSubject> findByNameStartingWith(String name);
Page<PSubject> findBy(Pageable page);
#Query("name:*?0* or description:*?0* or type:*?0* or mac_address:*?0* or uri:*?0* or attributes:*?0*")
Page<PSubject> find(String keyword,Pageable page);
#Query("name:*?0* or description:*?0* or type:*?0* or mac_address:*?0* or uri:*?0* or attributes:*?0*")
List<PSubject> find(String keyword);
}
I can search any by name, description, type and mac_address, but can't search any result by attribute.
Update:
For example,when user search "ipod", it's probably means the type of subject or name of subject, or the name of attribute or the value of attribute. And I want get all the matched subject in one request. I know I can search the attribute object in a separate query. But that makes the code in the backend complex.
So, how can I search this nested object?
Update:
I flattened my data:
#Transient
#Field(index=Index.YES, analyze=Analyze.YES, store=Store.NO)
#org.apache.solr.client.solrj.beans.Field("attrs")
private String attrs;
public String getAttrs() {
return attrs;
}
public void setAttrs(Set<PAttribute> attributes) {
StringBuffer attrs = new StringBuffer();
if(attributes==null) {
attributes = this.getAttributes();
}
for(PAttribute attr:attributes){
attrs.append(attr.getName()+" " + attr.getValue()).append(" ");
}
this.attrs =attrs.toString();
}
The issue is resolved.
IIRC it is not possible to store nested data structures in solr - it depends how you flatten your data to fit into an eg. multivalue field - a little hard not knowing your schema.
see: http://lucene.472066.n3.nabble.com/Possible-to-have-Solr-documents-with-deeply-nested-data-structures-i-e-hashes-within-hashes-td4004285.html
How does the data look like in you index, and did you have a look at the http request sent by spring-data-solr?

Resources