Optimistic locking not working with spring data couchbase #version - spring-data

As mentioned in spring data couchbase reference guide, i am trying to enable optimistic locking feature using #Version annotation. My expectation is couchbase will populate version field when a doucument is mutated. But seems like it is not populating the version field. its always 0. Following is my Pojo, and using crudRepository to save the document. when i update i try to send version as 1 to simulate optimitic locking exception. But i didn't get any exception the update went fine. Since the documentation is not helping much, i could not proceed further. Any help will be greatly appreciated?
#Document
public class Project extends BusinessEntity {
private static final long serialVersionUID = 1665165729053936288L;
#NotNull
#Field
private String name;
#Field
private String description;
#Version
private long version;
public Project() {
}
public Project(String name) {
this.name = name;
}
public Project(String name, String description) {
super();
this.name = name;
this.description = description;
}
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the description
*/
public String getDescription() {
return description;
}
/**
* #param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* #return the version
*/
public long getVersion() {
return version;
}
/**
* #param version the version to set
*/
public void setVersion(long version) {
this.version = version;
}
}

Its my fault. Couchbase populates version field.But we are requried to send version number while saving.

Another reason this could fail is that you need to have a getter for your #version field.

Related

Applying licensing and author to java class

I am trying out Visual Paradigm for the first time. I have created a class diagram and used the Update Code button in the diagram navigator to create a java class. The issue I have is, when the class code is created, the license and author information is not being applied to the file. Is there any way to get Netbeans to insert the information when creating the file with the Visual Paradigm interface?
I used the Visual Paradigm plugin for NetBeans to create a class diagram and then used the Update Code tool to generate the Java class. But as I stated above, the licensing and author information are missing.
package onlineshop;
public class Product {
private String name;
private String partNo;
private float price;
public Product(String name, String partNo, float price) {
this.name = name;
this.partNo = partNo;
this.price = price;
}
public String getName() {
return this.name;
}
/**
*
* #param name
*/
public void setName(String name) {
this.name = name;
}
public String getPartNo() {
return this.partNo;
}
/**
*
* #param partNo
*/
public void setPartNo(String partNo) {
this.partNo = partNo;
}
public float getPrice() {
return this.price;
}
/**
*
* #param price
*/
public void setPrice(float price) {
this.price = price;
}
}

translate sql to JPQL

i m a total newbie to JPQL ,so i m working on a Spring Boot app and i have this SQL query part :
select TOP 10 RFC_NUMBER, RECIPIENT_ID from [50004].SD_REQUEST S
INNER JOIN [50004].AM_EMPLOYEE E
--ON S.RECIPIENT_ID = E.EMPLOYEE_ID
WHERE E.AVAILABLE_FIELD_5 ='j.doe'
AND SD_REQUEST.STATUS_ID NOT IN (8,6,18,7,24)
AND SD_REQUEST.RFC_NUMBER like 'I%'
to JPQL.
i tried doing a #Query like this :
#Query("select x from Incident x Left join x.recipient recip where recip.login=:login and (x.rfcnumber like :I_% or :rfcnumber = null )"
+ " and x.status NOT IN (8,6,18,7,24)")
but it only returns ALL the rfcnumber of the that employee , i want it to extract only the rfc number starting with letter I ,
i tried doing CONCAT from searching around in then web, same thing.
i m new to this so i figure it'll be something much simpler , i m thinking it's just syntax problem .
Thanks a bunch.
Edit (adding models):
import java.io.Serializable;
import java.sql.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
#Entity
#Table(name="SD_REQUEST")
public class Incident implements Serializable {
private static final long serialVersionUID = -8235081865121541091L;
#Id
#Column(name="REQUEST_ID")
private Integer inid;
#ManyToOne
#JoinColumn(name = "SUBMITTED_BY")
private Employee sender;
#Column(name="RFC_NUMBER")
private String rfcnumber;
#Column(name="CREATION_DATE_UT")
private Date date;
#Column(name="DESCRIPTION")
private String description;
#Column(name="COMMENT")
private String comment;
#Column(name="STATUS_ID")
private Integer status;
#ManyToOne
#JoinColumn(name = "RECIPIENT_ID")
private Employee recipient;
public Incident()
{
}
public Incident(int inid,String rfcnumber,Date date,String description,String comment,Integer status)
{
this.inid=inid;
this.rfcnumber= rfcnumber;
this.date=date;
this.description=description;
this.comment=comment;
this.status=status;
}
public int getInid() {
return inid;
}
public void setInid(int inid) {
this.inid = inid;
}
public String getRfcnumber() {
return rfcnumber;
}
public void setRfcnumber(String rfcnumber) {
this.rfcnumber = rfcnumber;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Employee getSender() {
return sender;
}
public void setSender(Employee sender) {
this.sender = sender;
}
public Employee getRecipient() {
return recipient;
}
public void setRecipient(Employee recipient) {
this.recipient = recipient;
}
public void setInid(Integer inid) {
this.inid = inid;
}
}
And here's the model for Employee :
import javax.persistence.*;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
#Entity
#JsonDeserialize(as =Employee.class)
#Table(name = "AM_EMPLOYEE")
public class Employee implements Serializable {
private static final long serialVersionUID = 5071617893593927440L;
#Id
#Column(name = "EMPLOYEE_ID" )
private Integer id;
#Column(name = "LAST_NAME")
private String lastName;
#Column(name = "AVAILABLE_FIELD_5")
private String login;
#OneToMany(mappedBy="sender")
#JsonIgnore
private List<Incident> myCreatedIncidents;
#OneToMany(mappedBy="recipient")
#JsonIgnore
private List<Incident> myOtherIncidents;
#Column(name = "PASSWD")
private String password;
public Employee() {
//super();
}
public Employee (String login,String password)
{
}
public Employee(Integer id, String lastName,String login, String password) {
this.id = id;
this.lastName = lastName;
this.login = login;
this.password = password;
}
/**
* #return the id
*/
public Integer getId() {
return id;
}
/**
* #param id
* the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* #return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* #param lastName
* the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* #return the login
*/
public String getLogin() {
return login;
}
/**
* #param login
* the login to set
*/
public void setLogin(String login) {
this.login = login;
}
/**
* #return the password
*/
public String getPassword() {
return password;
}
/**
* #param password
* the password to set
*/
public void setPassword(String password) {
this.password = password;
}
public List<Incident> getMyCreatedIncidents() {
return myCreatedIncidents;
}
public void setMyCreatedIncidents(List<Incident> myCreatedIncidents) {
this.myCreatedIncidents = myCreatedIncidents;
}
public List<Incident> getMyOtherIncidents() {
return myOtherIncidents;
}
public void setMyOtherIncidents(List<Incident> myOtherIncidents) {
this.myOtherIncidents = myOtherIncidents;
}
}
Hard-coded characters
I think you should use the same as in SQL:
like 'I%'
Specifically, according to the article # http://www.objectdb.com/java/jpa/query/jpql/string#LIKE_-_String_Pattern_Matching_with_Wildcards_ :
The percent character (%) - which matches zero or more of any character.
Blockquote
So try the following:
#Query("select x from Incident x Left join x.recipient recip where recip.login=:login and (x.rfcnumber like 'I%' or :rfcnumber = null )"
+ " and x.status NOT IN (8,6,18,7,24)"
)
Parameters
See the solutions # Parameter in like clause JPQL if you are using a parameter.
Examples:
LIKE :code%
Also other examples are included in the stackoverflow question.
#Query("select x from Incident x where x.recipient.login=:login and (x.rfcnumber like I% or x.rfcnumber = null )"
+ " and x.status NOT IN (8,6,18,7,24))"
try this query

spring data not get id annotaion on method

When my Entity id annotation on field, it's OK on spring data jpa.
public class User {
#Id
#Column(name="id")
Long rid;
#Column(name="name")
String name;
}
but not work on method...
public class User {
Long rid;
String name;
#Id
#Column(name="id")
public Long getId() {
return this.id;
}
#Column(name="name")
public Long getName() {
return this.name;
}
}
the error message is PersistentEntity does not have an identifier property!
why?
I need to set annotation on method, because I want to set association auto.
#OneToMany(mappedBy="user")
#Cascade(CascadeType.ALL)
public Set<Phone> getPhones() {
return smtpRecords;
}
public void setPhones(Set<Phone> phones) {
phones.forEach(e -> e.setUser(this));
this.phones= phones;
}
any solution? Thanks.

Spring boot integration with Postgres jsonb

I am writing an application which is based on spring boot and tries to store and retrieve data from PostGreSQL db in its jsonb column.
while saving the record it works fine but the moment i write basic method to find records in repository interface of it like this:-
public interface AgentProfileRepository extends CrudRepository<AgentProfileOuter,String> {
public AgentProfileOuter findByJdataPcpAgentId(String id);
}
then server starts giving this exception while restarting:-
Caused by: java.lang.IllegalStateException: Illegal attempt to dereference path source [null.jdata] of basic type
at org.hibernate.jpa.criteria.path.AbstractPathImpl.illegalDereference(AbstractPathImpl.java:98) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.criteria.path.AbstractPathImpl.get(AbstractPathImpl.java:191) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:524) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:478) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.getTypedPath(JpaQueryCreator.java:300) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:243) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:148) ~[spring-data-jpa-1.9.4.RELEASE.jar:na]
The point to wonder is when I try to find by id which is a normal numeric column in postgres it works fine but not if i try to find by a key inside json.This thing works successfully with MongoDB.
Here are the bean classes written:-
AgentProfileOuter.java
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
#Table(name = "Agentbonds")
#Entity
public class AgentProfileOuter {
#GeneratedValue(strategy=GenerationType.AUTO)
#Id
private long id;
#Convert(converter = ConverterAgent.class)
#Column(name="jdata")
private AgentProfile jdata;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public AgentProfile getJdata() {
return jdata;
}
public void setJdata(AgentProfile jdata) {
this.jdata = jdata;
}
}
AgentProfile.java
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Table;
import org.springframework.data.annotation.Id;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
#JsonInclude(JsonInclude.Include.NON_NULL)
#Generated("org.jsonschema2pojo")
#JsonPropertyOrder({
"pcpAgentId",
"name",
"bio",
"phone",
"email",
"sms",
"imageUrl"
})
public class AgentProfile {
#JsonProperty("pcpAgentId")
private String pcpAgentId;
/*
public void setAdditionalProperties(Map<String, Object> additionalProperties) {
this.additionalProperties = additionalProperties;
}
*/
#JsonProperty("name")
private String name;
#JsonProperty("bio")
private String bio;
#JsonProperty("phone")
private String phone;
#JsonProperty("email")
private String email;
#JsonProperty("sms")
private String sms;
#JsonProperty("imageUrl")
private String imageUrl;
#JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* #return
* The pcpAgentId
*/
#JsonProperty("pcpAgentId")
public String getpcpAgentId() {
return pcpAgentId;
}
/**
*
* #param pcpAgentId
* The pcpAgentId
*/
#JsonProperty("pcpAgentId")
public void setAgentId(String pcpAgentId) {
this.pcpAgentId = pcpAgentId;
}
/**
*
* #return
* The name
*/
#JsonProperty("name")
public String getName() {
return name;
}
/**
*
* #param name
* The name
*/
#JsonProperty("name")
public void setName(String name) {
this.name = name;
}
/**
*
* #return
* The bio
*/
#JsonProperty("bio")
public String getBio() {
return bio;
}
/**
*
* #param bio
* The bio
*/
#JsonProperty("bio")
public void setBio(String bio) {
this.bio = bio;
}
/**
*
* #return
* The phone
*/
#JsonProperty("phone")
public String getPhone() {
return phone;
}
/**
*
* #param phone
* The phone
*/
#JsonProperty("phone")
public void setPhone(String phone) {
this.phone = phone;
}
/**
*
* #return
* The email
*/
#JsonProperty("email")
public String getEmail() {
return email;
}
/**
*
* #param email
* The email
*/
#JsonProperty("email")
public void setEmail(String email) {
this.email = email;
}
/**
*
* #return
* The sms
*/
#JsonProperty("sms")
public String getSms() {
return sms;
}
/**
*
* #param sms
* The sms
*/
#JsonProperty("sms")
public void setSms(String sms) {
this.sms = sms;
}
/**
*
* #return
* The imageUrl
*/
#JsonProperty("imageUrl")
public String getImageUrl() {
return imageUrl;
}
/**
*
* #param imageUrl
* The imageUrl
*/
#JsonProperty("imageUrl")
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
#JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
#JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
Any help on this is greatly appreciated.
I think, it's because how both Mongo and PostGreSQL structure the data. From Mongo's point, AgentProfileOuter is one document saved in JSON format as key:value. Every field in your AgentProfile class is key for Mongo irrespective of the fact it's another/ child object. However, for PostGreSQL whole AgentProfile object is just one column of String blob, since you have not marked this class as #Entity and it does not have a primary id. So, when you try to search something like pcpAgentId=someid, it does not make any sense to PostGreSQL. This is my guess, verify by checking your data structure in PostGreSQL.
Also noticed that CrudRepository<AgentProfileOuter,String> should be like CrudRepository<AgentProfileOuter,long> since AgentProfilOuter class's primary key is long.

JPA #Id annoation

i want to insert into a table with a specified value,but it just don't work,
here is my code:
#Id
#Column(insertable=true,updatable=true)
public Long getS_id() {
return s_id;
}
#Resource(name="studentService")
private StudentService stus;
Student student = new Student();
student.setS_id(123213L);
student.setName("vincent");
stus.add(student);
If I change:
#Id
#Column(insertable=true,updatable=true)
public Long getS_id() {
return s_id;
}
to this:
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(insertable=true,updatable=true)
public Long getS_id() {
return s_id;
}
and don't set s_id manualy it works well.
here my student class
#Entity()
#Table(name="stu_info")
public class Student implements Serializable{
private static final long serialVersionUID = 1L;
/**
* 学生的学号
*/
private Long s_id;
/**
* 学生姓名
*/
private String name;
/**
* 学生性别
*/
private String sex;
/**
* 学生生日
*/
private Date birthday;
/**
* 学生电话号码
*/
private String telephone;
/**
* 学生所在年级
*/
private String grade;
/**
* 学生所在班级
*/
private String classes;
/**
* 学生编号
*/
private int number;
/**
* 学生父亲姓名
*/
private String father_name;
/**
* 学生母亲姓名
*/
private String mother_name;
/**
* 学生个人疾病史
*/
private String diseases_history;
#Id
#Column(insertable=true,updatable=true)
public Long getS_id() {
return s_id;
}
public void setS_id(Long s_id) {
this.s_id = s_id;
}
#Column(length=32)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Column(length=12)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
#Temporal(TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
#Column(length=12)
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
#Column(length=32)
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
#Column(length=32)
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
#Column(length=32)
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
#Column(length=32)
public String getFather_name() {
return father_name;
}
public void setFather_name(String father_name) {
this.father_name = father_name;
}
#Column(length=32)
public String getMother_name() {
return mother_name;
}
public void setMother_name(String mother_name) {
this.mother_name = mother_name;
}
#Column(length=32)
public String getDiseases_history() {
return diseases_history;
}
public void setDiseases_history(String diseases_history) {
this.diseases_history = diseases_history;
}
}
From the limited information posted I would guess you are using SQL Server and to insert a record into an SQLServer table with an explicit value defined for an Identity column requires you to turn on identity inserts for that table.
https://msdn.microsoft.com/en-gb/library/ms188059.aspx
So if you run the above against your table you should then be able to persist using a specific value.
So not really anything to do with JPA.