Spring Data Solr and Polymorphism - java

i have defined destination class and some subclasses like region, country, city with some additional fields. i want put all their instances in the same solr core. it works, because i have created the schema with all possible fields(regionName, countryName etc. in the same schema.xml).
but i don't know, how can i fetch them and convert in the correct type? has anyone any idea? or it is not possible?
thanks for your answers,
best regards
shan
the code likes:
Destiantion
#SolrDocument(solrCoreName = "destination")
public class Destination implements Serializable {
/**
*
*/
private static final long serialVersionUID = 4593263425568053104L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
City
public class City extends Destination {
/**
*
*/
private static final long serialVersionUID = 4593263425568053104L;
private int regionId;
private String regionName;
public int getRegionId() {
return regionId;
}
public void setId(int regionId) {
this.regionId = regionId;
}
public String getRegionName() {
return regionName;
}
public void setName(String regionName) {
this.regionName = regionName;
}
}

What kind of a code is this??
You use
#Field
on top of the fields/methods for no reason.
You write
Integer
instead of
int
in the return type. Dude ar e you trolling or what?
And the answer is NO.
(Had to write NO in case this post gets deleted.)
(I believe your post is ridicilious and deserves the answer NO)
(That's how it works in stack overflow nowadays.)

Related

How does Hibernate fetch data in a OneToMany relationship under the hood?

I am developing an ORM library similar to Hibernate. Now I'm stuck with the OneToMany relationship. I'd like to know how to fetch automatically data from database when getter of the one side is called and how Hibernate does it under the hood.
Many side
public class Film {
private int id;
private String name;
#JoinColumn(name="producer_id")
private Producer producer;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Producer getProducer() {
return producer;
}
public void setProducer(Producer producer) {
this.producer = producer;
}
}
One Side
public class Producer {
#Id
private int id;
private String name;
#OneToMany(mappedBy="producer")
private Set<Film> films;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
// When called, it executes: SELECT ... FROM Film where producer_id = ?
public Set<Film> getFilms() {
return films;
}
}
In other words, I want to fill films inside Producer only when getFilms() is called.
Hibernate uses proxies of entity classes instead of real entity class using bytebuddy by generating addintional code at runtime.
BTW, I am just curious why are you developing your own ORM when you can use hibernate itself? It's the best ORM out there covering almost all kind of use cases and different optimization techniques.

Realm for Android: Many to Many Relationship

I am trying to learn Realm basics by implementing a simple Android project.
The idea is that user have several items and several item lists and an item can be added to any of these lists and a list can have many items. Therefore, there is a many to many relationship between Item and List objects. Here are my objects.
public class Item extends RealmObject {
#PrimaryKey
private String id;
private String name;
private boolean isDone;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isDone() {
return isDone;
}
public void setDone(boolean done) {
isCollected = done;
}
}
public class List extends RealmObject {
#PrimaryKey
private String id;
private String name;
private RealmList<Item> items;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public RealmList<Item> getItems() {
return items;
}
public void setItems(RealmList<Item> items) {
this.items = items;
}
}
My problem is, the field isDone might be different depending on the item's status in a given list. But when I update this field in a list, all the other items added to different lists get updated too. Since they are using the same object it makes sense but that is not to behavior I want. Is there a Realm way to create a pivot table/object with an extra column/field (in that case isDone) ?
Any help would be appreciated.
The problem is that the isDone property of Item doesn't truly belong to Item. If you can set the same Item in multiple Lists, then the property that a given task is deemed complete within a given List is the property of the List.
public class Item extends RealmObject {
#PrimaryKey
private String id;
private String name;
//private boolean isDone;
#LinkingObjects("completedItems")
private final RealmResults<List> tasksCompletedIn = null;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//public boolean isDone() {
// return isDone;
//}
//public void setDone(boolean done) {
// isDone = done;
//}
}
public class List extends RealmObject {
#PrimaryKey
private String id;
private String name;
private RealmList<Item> items;
private RealmList<Item> completedItems;
Then you know if it's a completed item if completedItems.contains(item) (where item is a managed object, or overrides equals to check against only id)

Ref.get returning null

I am learning google app engine with datastore for my next project. I have made a sample app for the same.
Here are the code for entities:
#Entity
public class Quote {
#Id
private Long id;
#Parent #Load
private Ref<Author> author;
public Quote() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Author getAuther() {
return author.get();
}
public void setAuther(Author author) {
this.author = Ref.create(author);
}
}
#Entity
public class Author {
#Id
private Long id;
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;
}
}
and I am inserting a Quote using this API
#ApiMethod(
name = "insert",
path = "quote",
httpMethod = ApiMethod.HttpMethod.POST)
public Quote insert(Quote quote) {
ofy().save().entity(quote).now();
return ofy().load().entity(quote).now();
}
When I try to insert a new quote, I get my author.get() as null. I am stuck in this problem from a long time and I am not able to continue learning.
Thanks.
I was not inserting Auther before inserting Quote. You can either hide it within the Entity model or you can do it separately in an API call.

spring mvc use model.addAttribute(nav) can not get the parent class’s attributes in jsp with ${nav.id}

i am new Spring learner.
i'm really confused about model.addAttribute
how can i get all attributes(include parent)
in below there are my code,see this code,please:
Controller:
#RequestMapping("/nav/addOrEdit")
public String navAdd(Nav nav,ModelMap model){
if(nav.getId()!=null&&nav.getId()!=0) {
nav=siteService.getNav(nav.getId());
model.addAttribute(nav);
System.out.println("nav.id:"+nav.getId());
}
return "CJ/nav/addOrEdit";
}
Nav:
public class Nav extends PO{
private static final long serialVersionUID = -13569672251584L;
protected String code;
#NotEmpty
protected String name;
public String getCode() { return code; }
public void setCode(String code) { this.code = code; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
}
PO:
public class PO implements Serializable {
private static final long serialVersionUID = 4572077184754045588L;
protected Long Id;
public Long getId() {return Id;}
public void setId(Long id) {Id = id;}
}
jsp:
console.log("${nav}"); -->Nav{code=1, name=test},
no id,how can i get the parent class’s attributes?
In your case you are adding your parent class object in Map. So you can not directly access that object instead of putting object directly into Map add that object with some key and directly access it on JSP. i.e.
model.addAttribute("nav",nav);
More details check Spring Documention of Model interface
Now you can access your value directly.

How to map the simplevalue type of Set collection using annotation

Could anyone please explain me how to use Simple value type of Set, by using annotation mapping without using one to many relationship. I have shown example in this below code. In this code I have person name and person petnames, here the person petname is the simple value type of the Set. I want map to the person name in the one table in the one table and petnames in the other table.
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private Set<String> petname;
public Set<String> getPetname() {
return petname;
}
public void setPetname(Set<String> petname) {
this.petname = petname;
}
public boolean addPetNames(String a) {
return petname.add(a);
}
The JPA annotation works like this: (quite self explaining I think)
#ElementCollection
#CollectionTable(
name="PET_NAMES",
joinColumns=#JoinColumn(name="PERSON_ID")
)
#Column(name="PET_NAME")
private Set<String> petname;

Categories