Spring Data Couchbase Object id - java

I'm trying to create a Document with an #Id property different from String, everything works great. My issue is the way the document is stored in the Bucket, unfortunately it prefixes with the Class name:
ActorKey(key=d7471027-4bd1-40a3-8e29-5249f45beed4)
{
"name": "Emma Watson",
"_class": "com.guilherme.miguel.domain.Actor"
}
Is there a way to store the id like if it was a simple String?
This is to avoid issues in plain queries (simple queries will force me to use ActorKey(key=xxxxxxxxxxx))
Actor.java:
#Data
#AllArgsConstructor
#NoArgsConstructor
#Document
public class Actor {
#Id
private ActorKey key;
private String name;
}
ActorKey.java:
#Data
#AllArgsConstructor
#NoArgsConstructor
public class ActorKey implements Serializable {
private String key;
}
ActorRepository.java
#N1qlPrimaryIndexed
#ViewIndexed(designDoc = "actor")
public interface ActorRepository extends CouchbaseRepository<Actor, ActorKey> {
}

The underlying Couchbase SDK uses String as a Document identifier, so spring-data-couchbase will call toString() on your key objects. What you see is the result of the Lombok-generated toString() method (a generation that is part of the #Data annotation).
Simply explicitly override toString() in ActorKey!

Related

How can I make javax validation both of sub class and super class?

I'm using Spring framework,
and I faced the inheritance problem when I write Controller logic.
First of all,
this is my Controller code snippet
#PostMapping("/pay/detail")
public ResponseEntity<PayDetail.Response> getPayDetail(
#Valid PayDetail.Request payDetail
) {
... some code
}
and PayDetail class looks like this
public class PayDetail {
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
public static class Request extends CommReqForm {
#NotNull(message = "Not null please")
private String work_type;
}
}
and CommReqForm
#Data
#AllArgsConstructor
#NoArgsConstructor
public class CommReqForm {
#NotEmpty(message = "servicecode not empty")
private String servicecode;
#NotEmpty(message = "reqtype not empty")
private String reqtype;
}
I wish that I can validate both of PayDetail.Request and CommReqForm classes but It makes validation just only PayDetail.Request class.
How can I solve this problem?
#Valid cannot validate super class. I want to make both of sub class and super class validation.

How can I write SQL query in JPA repository with Spring Boot?

I'm doing a spring boot experiment and using MySQL.
For example, if I have a list of users, but I want to get the specified names, how can I write the SQL query that only indicates this situation?
This is my model class :
#Getter
#Setter
#NoArgsConstructor
#AllArgsConstructor
#Entity
#Table(name="users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
public long id;
#Column(name="first_name")
public String name;
#Column (name="last_name")
public String last_name;
}
This is my JPA interface :
public interface CommentRepository extends JpaRepository<User , Long >{
// All C.R.U.D database methods
}
Finally, my controller area is as below :
#RestController
#RequestMapping(path="/api/v1/users")
public class CommentController {
#Autowired
CommentRepository repository ;
#GetMapping(path="/list")
public List<User> users() {
return repository.findAll();
}
}
Maybe you didn't understand my problem, I just want to write a customizable query of my own.
For example, I want to pull the data with the method I designed, while I normally pull the data by numbers with the find by id method.
You can either use methods that will be translated into queries or write your queries in the #Query annotation.
Please read the docs: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories

How can I use JAXB to bind mulitple non-unique elements

I'm having this issue with parsing XML using JAXB. Here is a simplified layout of the XML in question:
<message>
<header>
<network>NET</network>
<sendTime>0722</sendTime>
</header>
<generalInformation>
<senderReference>1234</senderReference>
<linkage>
<externalReference>extRef</externalReference>
</linkage>
<linkage>
<internalReference>intRef</internalReference>
</linkage>
<linkage>
<systemReference>sysRef</externalReference>
</linkage>
</generalInformation>
</message>
The problem I'm having is that these references are being sent under the linkage tag which is not unique, and also doesn't have a root like "linkages" which would allow me to easily wrap it in a list in Java, because the generalInformation tag has other tags in it. Here is how I have it set up so far:
#XmlRootElement(name="message")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Message {
private Header header;
private GeneralInformation generalInformation;
}
#XmlRootElement(name="header")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Header {
private String network;
private String sendTime;
}
#XmlRootElement(name="generalInformation")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class GeneralInformation {
private String senderReference;
//How to create for linkages??
}
So my question to you is, how can I configure the GeneralInformation class to handle these multiple linkages? I am mostly concerned with unmarshalling from XML to Java at the moment.
Just define it as a List, for example:
private List<Linkage> linkage;
and define the Linkage class to have single String property:
#XmlRootElement(name="linkage")
...
public class Linkage {
private String systemReference;
}
#XmlRootElement(name="generalInformation")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class GeneralInformation {
private String senderReference;
private List<Linkage>;
}
#XmlRootElement(name="linkage")
#NoArgsConstructor
#AllArgsConstructor
#Data
public class Linkage {
private String externalReference;
private String internalReference;
private String systemReference;
}

Java Annotation for composite Range Keys in DDB

I have a my_table with a composite sort key made of two combined attributes id and model_name (i.e., id_model_name, similarly from what is done here here and here).
So I created this Java model:
#Builder
#Data
#AllArgsConstructor
#NoArgsConstructor
#DynamoDBTable(tableName = "my_table")
public class TableModel {
private static final String COMPOSITE_KEY_SEPARATOR = "_";
#DynamoDBAttribute(attributeName = "id")
private String id;
#DynamoDBAttribute(attributeName = "model_name")
private String modelName;
#DynamoDBRangeKey(attributeName = "id_model_name")
public String getIdModelName() {
return String.format("%s%s%s", id, COMPOSITE_KEY_SEPARATOR, modelName);
}
// more stuff...
}
However I'm getting:
DynamoDBMappingException: DRTFacet[id_model_name]; could not unconvert attribute
Notice that there is no String idModelName field, because it could mess up with #AllArgsConstructor and #Builder (since it's a derived field). Is it because this field is missing (together with a setter method?). How can I overcome this?
I found out that providing a dummy setter solved the problem:
public void setIdModelName(final String idModelName) {}

Get Mongo db id after insertion in case of POJO class

I have a POJO class in java
#Data
#EqualsAndHashCode(callSuper = false)
#NoArgsConstructor
#AllArgsConstructor
public class TestModel{
protected ObjectId id;
private String name;
}
Is there any way to populate id field in case of insertion. I can see that if I use Document, then after insertion I can get objectId by using
document.get("_id");
So is there any way I can achieve same using custom POJO class.

Categories