Is there a way to map fields from classes to one using Orika.
Can't find the solution in the orika documentation.
In the example the fields test & name from class ObjectOne should be mapped to the corresponding fields ObjectNew.
public class ObjectOne {
private String test;
private String name;
private String id;
public ObjectOne(String id,String test, String name){
this.id=id;
this.test=test;
this.name=name;
}
}
The same with field sheet from ObjectTwo
public class ObjectTwo {
private String sheet;
private String id;
public ObjectTwo(String id,String sheet){
this.id=id;
this.sheet=sheet;
}
}
Code for ObjectNew
public class ObjectNew {
private String id;
private String test;
private String name;
private String sheet;
public ObjectNew(String id,String test,String name,String sheet){
this.id=id;
this.test=test;
this.name = name;
this.sheet = sheet;
}
}
Fields from both classes ObjectOne & ObjectTwo should initiate new object ObjectNew when id's of classes ObjectOne and ObjectTwo are the same.
Any ideas how to handle this?
Kind Regards
I would suggest to wrap the source objects into one source wrapper object and map this new wrapper object with your new object:
public class objectWrapper{
private objectOne objectOne;
private objectTwo objectTwo;
}
Related
When I try to create a constructor in dart like Student(this._name) it doesn't work with private variables.
I have already tried using setters but it doesn't work either.
class Student{
var _id;
var _name;
Student(this.id, this.name);
void set id(int id) => _id = id;
void set name(String name) => _name = name;
}
This is not supported because it would expose private implementation to the outside.
If you'd rename var _id; to var _userId; you would break code that uses your class just by renaming a private field.
See instead the comment below my answer.
class Student{
var _id;
var _name;
Student({this._id, this._name}); // error
void set id(int id) => _id = id;
void set name(String name) => _name = name;
}
The alternative
class Student{
var _id;
var _name;
Student({int id, String name}) : _id = id, _name = name;
void set id(int id) => _id = id;
void set name(String name) => _name = name;
}
You can use this notation
class Student {
String _id;
String _name;
Student({required String id, required String name})
: _id = id,
_name = name;
}
Some of you maybe struggle if the class was inheritance, you just need add coma (,) after initialize your private.
Example
class Animal {
String _name;
int _age;
}
class Dog extends Animal {
String _race;
Dog(String name, int age, {String? race}) : _race = race ?? "Wild", super(name, age);
}
Hope this code can help you.
This notation is not valid because the variable is not private and its elements are just as accessible again.
DartLang says: AVOID wrapping fields in getters and setters just to be "safe".
I'm trying to use a java.lang.String as the #Id of a NodeEntity.
#NodeEntity(label = "MachineType")
public class MachineType {
#Id private String id;
....
It should be possible acording to the spring data neo4j docu:
While an id is still required on all entities, the behavior has been
simplified by introducing the new #Id annotation. It replaces both
#GraphId and the primary attribute and can be placed on any attribute
with a simple type.
When I'm trying to insert I get a:
{
"cause": null,
"message": "Id must be assignable to Serializable!: null"
}
Which is strange, because String implements the Serializable.
Anyone has an idea where to search next?
I think that you cannot use anything else as an ID. Keep in mind that this Long number will be reused if you delete the node.
I use UUID plugin to generate true unique keys and when I use spring-data-rest I use BackendIdConverter to change the id to the uuid for the resources that I expose.
Example:
Model:
#NodeEntity
#Data
public class Target {
#Id #GeneratedValue Long id; // <----Neo4j id
private String uuid; // <----My Key
#Version Long version;
private List<String> labels = new ArrayList<>();
#Relationship(type = "HAS_MEDIA", direction=Relationship.OUTGOING)
private List<Gallery> media = new ArrayList<>();
}
Convert resource id to my key:
#Component
public class MovieIdConverter implements BackendIdConverter {
#Autowired MovieRepo movieRepository;
#Override
public Serializable fromRequestId(String id, Class<?> entityType) {
Movie movie = movieRepository.findByUuid(id);
return (Serializable) movie.getId();
}
#Override
public String toRequestId(Serializable serializable, Class<?> aClass) {
Long id = (Long) serializable;
Optional<Movie> movie = movieRepository.findById(id);
if (movie.isPresent()) return movie.get().getUuid();
return null;
}
#Override
public boolean supports(Class<?> aClass) {
return Movie.class.equals(aClass);
}
}
I am trying to create a view with datas which combines two tables. I successfully implemented the join and datas are displaying properly by using spring data JPA join. Here my issue is that, when I am calling findAll() method from only one table, which returns all the data including joined table also,
I joined table Users model class like:
#Entity
#Table(name = "users")
public class Users implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
#Column(name = "username")
public String username;
#Column(name = "password")
public String password;
#Column(name = "privid")
public Integer privid;
#OneToMany(cascade = CascadeType.ALL,mappedBy="pid")
public Set<Privillages> priviJoin;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getPrivid() {
return privid;
}
public void setPrivid(Integer privid) {
this.privid = privid;
}
public Set<Privillages> getPriviJoin() {
return priviJoin;
}
public void setPriviJoin(Set<Privillages> priviJoin) {
this.priviJoin = priviJoin;
}
public Users() {
}
}
And my second model Privillages like,
#Entity
#Table(name = "Privillages")
public class Privillages implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
public Integer Id;
#Column(name = "pname")
public String pname;
#ManyToOne(optional = false)
#JoinColumn(name = "pid", referencedColumnName = "privid")
public Users pid;
public Integer getId() {
return Id;
}
public void setId(Integer id) {
Id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Users getPid() {
return pid;
}
public void setPid(Users pid) {
this.pid = pid;
}
public Privillages(){
}
}
And repository containing,
#Query("select u from Users u JOIN FETCH u.priviJoin p")
Set<Users> findByUsername();
These are all my codes, here i added. The thing is that, join is properly working with expected resultset. But when I call findAll() method , the it returns all the structure including both joined table.
I called my findAll function like,
#RequestMapping("/check")
public List<Users> check() {
return (List<Users>) userRepo.findAll();
}
But result is like I previously mentioned.Here I added its screenshot,
In this figure we can see that it returns the both table values instead of users table data.
Why is it happening like this?
You defined your domain type Users to contain a reference so it gets loaded as specified.
If you want something similar to a Users object but without the reference, you have two options:
Change the Users type to not contain a reference.
Use a different type, similar to Users but without the reference. There are multiple ways to do that, but probably the simplest and most helpful in the current situation is to use a projection. See https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
I encountered some difficulties during playing with neo4j. Firstly, when I try to delete defined #EntityModel, I get an exception (Please, forgive me the quality of pics, the exception messages are also in question title):
My Controller (this is just for testing purpouse):
#Controller
public class HomeController {
#Autowired
PersonRepository personRepository;
#RequestMapping(value="/", method = RequestMethod.GET)
public String loadPage(final Model model, final HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
Person person = new Person("My person");
personRepository.save(person);
personRepository.findOne(person.getId());
return "home";
}
}
And model:
#NodeEntity
public class Person {
#GraphId
private Long id;
private String name;
public Person() {}
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(final String name) {
this.name = name;
}
public Long getId() {
return id;
}
}
Configuration file:
#Configuration
#EnableTransactionManagement
#EnableNeo4jRepositories(basePackages = "com.springapp.mvc.repository")
#ComponentScan({"com.springapp.mvc"})
public class PersistenceConfig extends Neo4jConfiguration {
#Bean
public GraphDatabaseService graphDatabaseService() {
return new SpringRestGraphDatabase("http://localhost:7474/db/data");
}
}
My Repository:
public interface PersonRepository extends GraphRepository<Person> {
#Query("MATCH (n:Person{name: \"{namveValue}\"}) RETURN n;")
Person findByName(#Param("nameValue") final String name);
}
What am I doing wrong? I figured out that perhaps Person should implement org.neo4j.graphdb.Node and this is the source of these exceptions. However, having searched github repos I see that people do not implement this interface in their models. I have not found solution on stackoverflow so far.
Node exists in database but I cannot either delete it or save it. Please, help.
You are trying to see if a node with ID '0' exists as a person. Since the root node hasn't got a '__type__' property, the call will fail. SDN uses this property to determine the entity type of a node.
That being said, the exception seems to be caused by the following line:
if(! personRepository.exists(0L)) {
I have an id in the bean and mapped in orm.xml to database. For each id I have to lookup description from database and display. If I have enum values R:"RED" G:"GREEN". Struts does type conversion using EnumConverter. I just want to do similar stuff; if id = 123 what is the product description (lookup). Can I use Struts converter by setting action.properties or by extending StrutsTypeConverter to lookup description for an id = 123? Please let me know if there are any other suggestions.
In short:- Each id I want to lookup description; where description is not part of the bean.
Product Bean
public class Product{
private Long id;
private String description;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String planDescription) {
this.description = planDescription;
}
}
Manage Bean
public class Manage{
private Long id;
private String Currency;
..and so on
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
<setters>..<getters>
}
Admin Action uses Manage Bean for all fields on UI
public class ManageAction extends ActionSupport
{
private Collection<Item> allItems;
private Manage manage;
public Collection<Item> getAllItems() {
return allItems;
}
public void setAllItems(Collection<Item> allItems) {
this.allItems = allItems;
}
}
.jsp file action = ManageAction
<s:label key="color" value="#color" />
<s:label key="id" value="%{id}" />
Here the id = 123 and I wish value gets translated to text such as "3IN - 4W - Folded" <product description>
I can have id in the bean always and look up for description each time from a cache and translate and display.
For a enum G:"GREEN" struts conveter gets us G stored in database and GREEN displayed on screen.
similarly if it find id=123 and I wish "write a converter" which looks up in the cache (may be a Map of values) and displays description.