Comparison of JsonNode in javers - javers

Person Class
public class Person implements java.io.Serializable{
private static final long serialVersionUID = 1L;
int id;
Address address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
Person(int id, Address address){
this.id=id;
this.address=address;
}
}
Address Class
public class Address implements java.io.Serializable {
private static final long serialVersionUID = 1L;
String state;
String city;
CodeInfo codeInfo;
public CodeInfo getCodeInfo() {
return codeInfo;
}
public void setCodeInfo(CodeInfo codeInfo) {
this.codeInfo = codeInfo;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Address(String state, String city,CodeInfo codeInfo) {
this.state = state;
this.city = city;
this.codeInfo = codeInfo;
}
}
CodeInfo class
public class CodeInfo implements Serializable {
private static final long serialVersionUID = 1L;
String prop;
public String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
public CodeInfo(String prop) {
this.prop=prop;
}
}
JaversNewClass
public class JaversNewClass {
private static final ObjectMapper MAPPER = new ObjectMapper();
public static void main(String[] args) {
Diff diff =JaversInstance.getInstance().compare(
MAPPER.valueToTree(new Person(1,new Address("Delhi",null,new
CodeInfo(null)))),
MAPPER.valueToTree(new Person(2,new Address("Noida","SEZ",new
CodeInfo("abc"))))
);
System.out.println(diff.toString());
}
}
Diff:
* new object: com.fasterxml.jackson.databind.node.ObjectNode/#_children/address/_children/codeInfo/_children/prop
* changes on com.fasterxml.jackson.databind.node.ObjectNode/ :
- '_children/address/_children/city._value' property with value 'SEZ' added
- '_children/address/_children/codeInfo._children' map changes :
'prop' -> 'com.fasterxml.jackson.databind.node.ObjectNode/#_children/address/_children/city' changed to 'com.fasterxml.jackson.databind.node.ObjectNode/#_children/address/_children/codeInfo/_children/prop'
- '_children/address/_children/state._value' value changed from 'Delhi' to 'Noida'
- '_children/id._value' value changed from '1' to '2'
In the current scenario I have two JsonNodes which I am comparing through javers.
So firstly i need to know is it possible to compare the json nodes in this way.
Secondly, I need to know why i am getting the new object and irrelevant map change instead of the value change.
However it is able to detect the address changes properly. This case is detected when nested object is changed from null..

Related

org.neo4j.driver.v1.exceptions.ServiceUnavailableException: Connection to the database terminated

I want to connect Spring Boot with neo4j database, however, it returns an error like that. It says that the connection has been terminated. The error is as follow:
org.neo4j.driver.v1.exceptions.ServiceUnavailableException: Connection to the database terminated.
This is my Controller
#RequestMapping("/neo4j/Movie")
public class MovieController {
private final MovieRepository movieRepository;
public MovieController(MovieRepository movieRepository) {
this.movieRepository = movieRepository;
}
#GetMapping("/graph")
public List<Movie> graph() {
return (List<Movie>) movieRepository.findAll();
}
}
This is my Repository
#Repository
public interface MovieRepository extends Neo4jRepository<Movie,Long> {
#Query("MATCH(m:Movie)<-[relation:ActedIn]-(b:Actor) RETURN m,relation,b")
Collection<Movie> graph();
}
And the application.properties
spring.data.neo4j.username=neo4j
spring.data.neo4j.password=neo4j
spring.data.neo4j.uri=bolt://localhost:7687
NodeEntity of moview
#NodeEntity
public class Movie {
#Id
private int id;
private String title;
private String genre;
// #JsonIgnoreProperties("movie")
#Relationship(type = "ActedIn")
private List<Actor> actors;
// #Relationship(type = "ACTED_IN" , direction = Relationship.INCOMING)
// private List<Actress> actresses = new ArrayList<>();
public Movie() {
}
public List<Actor> getActors() {
return actors;
}
public void setActors(List<Actor> actors) {
this.actors = actors;
}
public Movie(int id, String title, String genre, List<Actor> actors) {
this.id = id;
this.title = title;
this.genre=genre;
this.actors=actors;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre=genre;
}
}
And NodeEntity of Actor
public class Actor {
#GraphId
private Long id;
private String name;
private int age;
public Actor() {
}
public Actor(Long id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
I also tried springboot + neo4j projects downloaded from github, and also followed the instructions from neo4j website, but the projects still failed on my computer, so is there any super tutorials for neo4j and springboot?

Problem with proxy when using #IdClass in Hibernate

For legacy reasons I have composite keys in my database, I use #IdClass to map them (before I used #Embeddedid but it had it's own bugs). However I have problem whenever I'm trying to save composite key entity that contains proxy as id field.
I made very simple example to isolate this problem (example uses Spring Data, but I think its irrelevant).
p.s. I know that calling .save is not necessary, but it should not cause exception.
#Entity
public class Image {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String fileName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
#Entity
public class Product {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
#Entity
#IdClass(ProductImage.ProductImageId.class)
public class ProductImage {
public static class ProductImageId implements Serializable {
private static final long serialVersionUID = 8542391285589067304L;
private Long product;
private Long image;
public ProductImageId() {
}
public Long getProduct() {
return product;
}
public void setProduct(Long product) {
this.product = product;
}
public Long getImage() {
return image;
}
public void setImage(Long image) {
this.image = image;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProductImageId that = (ProductImageId) o;
return Objects.equals(product, that.product) &&
Objects.equals(image, that.image);
}
#Override
public int hashCode() {
return Objects.hash(product, image);
}
}
#ManyToOne(fetch = FetchType.LAZY)
#Id
private Product product;
#ManyToOne(fetch = FetchType.LAZY)
#Id
private Image image;
private Integer number;
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
List<ProductImage> productImages = productImageRepository.findAll();
productImages.get(0).setNumber(456);
productImageRepository.save(productImages.get(0));
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type 'com.example.springbootnewplayground.Product$HibernateProxy$AJ38NiN6' to required type 'java.lang.Long' for property 'product': PropertyEditor [org.springframework.beans.propertyeditors.CustomNumberEditor] returned inappropriate value of type 'com.example.springbootnewplayground.Product$HibernateProxy$AJ38NiN6'
at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:258) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:585) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 69 common frames omitted

Gson set SerializedName

I am using retrofit for making requests to my server. I am trying to use the same Model for multiple requests and I want to send different objects with different SerializedName.
My pojo looks like this:
public class BaseModel<T> implements Serializable {
#SerializedName("success")
#Expose
private boolean succcess;
#SerializedName("data")
#Expose private T data;
public boolean isSucccess() {
return succcess;
}
public void setSucccess(boolean succcess) {
this.succcess = succcess;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
How can I set my #SerializedName("data") in a dynamic way? Thank you all for your time!
Code sample:
public class BaseRequestModel<T> implements Serializable {
#SerializedName("success")
#Expose
private boolean success;
#Expose private T data;
public boolean isSucccess() {
return success;
}
public void setSucccess(boolean succcess) {
this.success = succcess;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
One of the objects that I am sending to BaseModel (as T data):
public class User implements Serializable {
transient String requestName = "user";
#SerializedName("id")
#Expose
private int id;
#SerializedName("owner_id")
#Expose private int ownerId;
#SerializedName("first_name")
#Expose private String firstName;
#SerializedName("middle_name")
#Expose private String middleName;
#SerializedName("last_name")
#Expose private String lastName;
#SerializedName("username")
#Expose private String username;
public String getRequestName() {
return requestName;
}
public void setRequestName(String requestName) {
this.requestName = requestName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getOwnerId() {
return ownerId;
}
public void setOwnerId(int ownerId) {
this.ownerId = ownerId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
My custom serializer:
public class BaseRequestCustomSerializer<T> implements JsonSerializer<User> {
#Override
public JsonElement serialize(User src, Type typeOfSrc, JsonSerializationContext context) {
src.getRequestName();
return null;
}
}
I have to pass to the serializer instead of the specific object, since this is what my problem is all about. Any ideas? Thank you!
if you want to set #SerializedName("data") than you must be get same variable name from api response.

Repeated column in mapping for entity: Shipper column: SHIPPER_ID (should be mapped with insert="false"

I have been going around in circles with this error and not sure why I am getting this.
Here is the mapping of Shipper class
#Entity
#Table(schema="SALONBOOKS",name="SHIPPER")
#AttributeOverride(name="id", column=#Column(name="SHIPPER_ID"))
public class Shipper extends SalonObject {
private static final long serialVersionUID = 1L;
private ShipperType name;//ShipperType.WALKIN;
#Column(name="SHIPPER_NAME")
#Enumerated(EnumType.STRING)
public ShipperType getName() {
return name;
}
public void setName(ShipperType name) {
this.name = name;
}
#Override
public Long getId(){
return id;
}
}
Here is Order class which references Shipper
#Entity
#Table(schema="SALONBOOKS",name="ORDER")
#AttributeOverride(name="id", column=#Column(name="ORDER_ID"))
public class Order extends SalonObject {
private static final long serialVersionUID = 1L;
private BigDecimal total= new BigDecimal(0.0);
private int numOfItems=0;
private BigDecimal tax= new BigDecimal(0.0);;
private String currency="USD";
private BigDecimal subTotal= new BigDecimal(0.0);
private PaymentMethod paymentMethod;
private Shipper shipper;
private OrderStatusType status;
private Appointment appointment ;
private Person person;
#Column(name="TOTAL")
public BigDecimal getTotal() {
return total;
}
public void setTotal(BigDecimal total) {
this.total = total;
}
#Column(name="NUM_OF_ITEMS")
public int getNumOfItems() {
return numOfItems;
}
public void setNumOfItems(int numOfItems) {
this.numOfItems = numOfItems;
}
#Column(name="TAX")
public BigDecimal getTax() {
return tax;
}
public void setTax(BigDecimal tax) {
this.tax = tax;
}
#Column(name="CURRENCY")
public String getCurrency() {
return currency;
}
public void setCurrency(String currency) {
this.currency = currency;
}
#Column(name="SUBTOTAL")
public BigDecimal getSubTotal() {
return subTotal;
}
public void setSubTotal(BigDecimal subTotal) {
this.subTotal = subTotal;
}
#ManyToOne
#JoinColumn(name="PAYMENT_METHOD_ID", insertable=false,updatable=false)
public PaymentMethod getPaymentMethod() {
return paymentMethod;
}
public void setPaymentMethod(PaymentMethod paymentMethod) {
this.paymentMethod = paymentMethod;
}
#ManyToOne
#JoinColumn(name="SHIPPER_ID", insertable=false,updatable=false)
public Shipper getShipper() {
return shipper;
}
public void setShipper(Shipper shipVia) {
this.shipper = shipVia;
}
#Column(name="STATUS")
#Enumerated(EnumType.STRING)
public OrderStatusType getStatus() {
return status;
}
public void setStatus(OrderStatusType status) {
this.status = status;
}
#ManyToOne
#JoinColumn(name="APPOINTMENT_ID", insertable=false,updatable=false)
public Appointment getAppointment() {
return appointment;
}
public void setAppointment(Appointment appointment) {
this.appointment = appointment;
}
#ManyToOne
#JoinColumn(name="PERSON_ID", insertable=false,updatable=false)
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
#Override
public Long getId(){
return id;
}
}
each of these extends:
#MappedSuperclass
public abstract class SalonObject implements Entity, Serializable {
private static final long serialVersionUID = 1L;
protected Long id;
protected DateTime createDate;
protected DateTime updateDate;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
#Override
public boolean equals(Object obj) {
if (obj instanceof SalonObject
&& obj !=null){
return ObjectUtils.equals(this.id, ((SalonObject) obj).getId()) ;
}
return false;
}
#Column(name="CREATE_DATE")
public DateTime getCreateDate() {
return createDate;
}
public void setCreateDate(DateTime dateTime) {
this.createDate = dateTime;
}
#Column(name="UPDATE_DATE")
public DateTime getUpdateDate() {
return updateDate;
}
public void setUpdateDate(DateTime updateDate) {
this.updateDate = updateDate;
}
}
The stackTrace is ::
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: salonbooks.model.Shipper column: SHIPPER_ID (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:709)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:731)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:753)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:506)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1358)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1849)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:343)
at salonbooks.core.HibernateConfiguration.sessionFactory(HibernateConfiguration.java:109)
removing the following method from Shipper and from Order worked to resolve this error
#Override
public Long getId(){
return id;
}
Because you are using property access, by overriding the base method (containing the mapping configuration) you will replace your base method mapping configuration with no config at all.
Using field access wouldn't have caused this issue, but the override would have been useless anyway. The id field should have private access too, so this method wouldn't compile if you change the access modifier.

Spring JPA repository query can't find ID property

I need a spring repository method that lets me get a list of Scene entities using a list of id's. When I try to refer to the Scene Id I get an error, saying it can't find the property called IdScene. I am using a custom query to do this. Is there something wrong with my query?
My Entity is
public class Scene implements Serializable
{
private long id_scene;
private Media media;
private int sceneNumber;
private int version;
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = IDENTITY)
#Column(name = "id_scene")
public long getIdScene() {
return id_scene;
}
public void setIdScene(long id_scene) {
this.id_scene = id_scene;
}
#ManyToOne
#JoinColumn(name = "id_media")
public Media getMedia() {
return this.media;
}
public void setMedia(Media media) {
this.media = media;
}
private List<Thumbnail> thumbnails = new ArrayList<Thumbnail>();
#OneToMany(mappedBy = "scene", cascade=CascadeType.ALL,
orphanRemoval=true)
#LazyCollection(LazyCollectionOption.FALSE)
public List<Thumbnail> getThumbnails() {
return this.thumbnails;
}
public void setThumbnails(List<Thumbnail> thumbnails) {
this.thumbnails = thumbnails;
}
public void addThumbnail(Thumbnail thumbnail) {
thumbnail.setScene(this);
this.thumbnails.add(thumbnail);
}
private Property property;
#OneToOne(mappedBy="scene", cascade=CascadeType.ALL,
orphanRemoval=true)
#LazyCollection(LazyCollectionOption.FALSE)
public Property getProperty() {
return property;
}
public void setProperty(Property property) {
this.property = property;
}
public void addProperty(Property property) {
property.setScene(this);
this.property = property;
}
#Column(name = "sceneNumber")
public int getSceneNumber() {
return sceneNumber;
}
public void setSceneNumber(int sceneNumber) {
this.sceneNumber = sceneNumber;
}
#Column(name = "version")
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
}
My repository:
public interface SceneRepository extends JpaRepository<Scene, Long> {
public final static String FIND_BY_ID_LIST = "SELECT s"
+ " FROM Scene s WHERE s.IdScene IN (:id)";
#Query(FIND_BY_ID_LIST)
public List<Scene> findByIdScene(#Param("id") List<Long> id);//, Pageable page);
}
try:
"SELECT s FROM Scene s WHERE s.idScene IN (:id)"
note lower case 'i' in 'idScene'"
This is because of the Java Bean naming convention, a property defined as:
public String getWibble() { return wibble; }
public void setWibble(String value) { wibble = value; }
defines wibble and not Wibble

Resources