I have entities like this : User->Blague->Score where an user have numerous blagues(childs) and a blague have one score.
I'm trying to get the score from a blague, but when I write this :
User userPoster = ofy().load().type(User.class).id(5066549580791808L).now();
Blague blague = ofy().load().type(Blague.class).parent(userPoster).id(4609152743636992L).now();
Score score = ofy().load().type(Score.class).parent(blague).id(5735052650479616L).now();
resp.getWriter().println(userPoster);
resp.getWriter().println(blague);
resp.getWriter().println(score);
(The ids are corrects) score is null, unlike blague and userPoster. Why is it null ?
The entities are created like that :
#ApiMethod(
name = "addBlague",
path = "addBlague",
httpMethod = ApiMethod.HttpMethod.POST)
public void addBlague(
#Named("category") EnumCategory category,
#Named("type") EnumType type,
#Named("lenght") EnumLenght lenght,
#Named("keywords") List<String> keywords,
#Named("text") String text,
#Named("userId") Long userId){
Key<User> userKey = Key.create(User.class, userId);
Blague blague = new Blague(category, type, lenght, text, userKey);
ofy().save().entity(blague).now();
Key<Blague> blagueKey = Key.create(Blague.class, blague.getId());
Score score = new Score(0, 0, blagueKey);
ofy().save().entity(score).now();
for (String word : keywords) {
KeyWord keyword = new KeyWord(word, blagueKey);
ofy().save().entity(keyword);
}
}
and of course, the classes are registered.
How can I get the score from a blague ?
Thanks
Edit :
The Socre code :
package blagueur;
import java.util.HashMap;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Parent;
#Entity
#Index
#Cache
public class Score {
#Id
private Long id;
private int likes;
private int dislikes;
private HashMap<String, Boolean> voters;
#Parent
private Key<Blague> blagueKey;
private Score() {}
public Score(int likes, int dislikes, Key<Blague> blague) {
this.likes = likes;
this.dislikes = dislikes;
this.blagueKey = blague;
this.voters = new HashMap<String, Boolean>();
}
public Long getId() {
return id;
}
public int getLikes() {
return likes;
}
public int getDislikes() {
return dislikes;
}
public void addVote(Long userId, boolean like){
voters.put(String.valueOf(userId), like);
if (like){
likes++;
}
else{
dislikes++;
}
}
public void removeVote(Long userId){
String id = String.valueOf(userId);
boolean like = voters.get(String.valueOf(userId));
voters.remove(String.valueOf(userId));
if (like){
likes--;
}
else{
dislikes--;
}
}
public Key<Blague> getBlagueKey() {
return blagueKey;
}
}
You need to be very careful to maintain the key hierarchy.
You state that your intended entity hierarchy is User -> Blague -> Score, but this code below does not do that:
Key<User> userKey = Key.create(User.class, userId);
Blague blague = new Blague(category, type, lenght, text, userKey);
ofy().save().entity(blague).now();
Key<Blague> blagueKey = Key.create(Blague.class, blague.getId());
Score score = new Score(0, 0, blagueKey);
ofy().save().entity(score).now();
When you create the blagueKey, you do not use the userKey as the parent.
While I do not encourage the code style you have written, if you wanted to do so, you need to do this:
...
Key<Blague> blagueKey = Key.create(userKey, Blague.class, blague.getId());
...
In general, you should let objectify handle this for you by using Ref<User>, Ref<Blague> as the #Parent objects, or if you must create Key objects, use Key.create(T pojo) rather than managing Keys yourself.
Related
I am trying to achieve many-to-many relationship in Room Persistence Library. I am trying to create a Notes App with Tags.
The idea is:
A note will have multiple tags.
A tag will have multiple notes.
Show all notes in RecyclerView along with Tags.
To achieve this, I have created two models Note.java, Tag.java and TagJoin model to store the relationship b/w notes and tags. Achieving one-to-one is very easy using #Relation annotation.
Here are my models
#Entity(tableName = "notes")
public class Note {
#PrimaryKey
#NonNull
public final String id;
#ColumnInfo(name = "note")
public String note;
#Ignore
public List<Tag> tags;
#Ignore
public Note(String note) {
this(UUID.randomUUID().toString(), note);
}
public Note(String id, String note) {
this.id = id;
this.note = note;
}
public String getId() {
return id;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
public List<Tag> getTags() {
return tags;
}
public void setTags(List<Tag> tags) {
this.tags = tags;
}
#Entity(tableName = "note_tag_join",
primaryKeys = {"noteId", "tagId"},
foreignKeys = {
#ForeignKey(
entity = Note.class,
parentColumns = "id",
childColumns = "noteId",
onDelete = CASCADE),
#ForeignKey(
entity = Tag.class,
parentColumns = "id",
childColumns = "tagId",
onDelete = CASCADE)},
indices = {
#Index(value = "noteId"),
#Index(value = "tagId")
}
)
public static class TagJoin {
#NonNull
public final String noteId;
#NonNull
public final String tagId;
public TagJoin(String noteId, String tagId) {
this.noteId = noteId;
this.tagId = tagId;
}
}
}
Tags Model:
#Entity(tableName = "tags", indices = {#Index(value = "name", unique = true)})
public class Tag {
#PrimaryKey
#NonNull
public String id;
#ColumnInfo(name = "name")
public String name;
#Ignore
public Tag(String name) {
this(UUID.randomUUID().toString(), name);
}
public Tag(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Notes Dao:
#Dao
public interface NoteDao {
#Query("SELECT * FROM notes ORDER BY id DESC")
LiveData<List<Note>> getAllNotes();
#Insert
long insert(Note note);
#Update
void update(Note note);
#Delete
void delete(Note note);
#Query("DELETE FROM notes")
void deleteAll();
#Query("SELECT COUNT(*) FROM notes")
int getNotesCount();
#Query("SELECT notes.* FROM notes\n" +
"INNER JOIN note_tag_join ON notes.id=note_tag_join.noteId\n" +
"WHERE note_tag_join.tagId=:tagId")
List<Note> getAllNotesOfTag(String tagId);
#Insert
void insert(Note.TagJoin... joins);
#Delete
void delete(Note.TagJoin... joins);
}
So far everything is good. Now I want to show the Notes in RecyclerView but I can't find a way to fetch all Notes along with Tags at once. One way is, getting the tags of each note in onBindViewHolder method which I think is wrong as we have to query the db each time row is displayed.
Please provide me suggestions.
PS: I have followed the code provided in this article
https://commonsware.com/AndroidArch/previews/mn-relations-in-room
So the thing that i want to happen, is making the tableview update the data in the database after editing it. I wanted to use the SetOnEditCommit method here. The cell editing does work, but it never gets updated, with no error either. In the first place im a bit clueless if this method is actually efficient (probably not), since its hard to find some sources for this specific thing. And the sources that i found weren't really helpful. So it would be nice if someone had an idea as to why it doesn't update, or maybe provide an alternate option here.
The mentioned part:
columnType.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<UserDetails, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<UserDetails, String> event) {
updataData();
}
});
tableview.setItems(null);
tableview.setItems(data);
}
public void updataData() {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://37.128.148.113:3306/FYS", "FYS", "Kcj8g87~");
Statement con = connection.createStatement();
//connection
TablePosition pos = tableview.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
TableColumn col = pos.getTableColumn();
String data1 = (String) col.getCellObservableValue(row).getValue();
//cell
UserDetails row1 = tableview.getSelectionModel().getSelectedItem();
c1 = row1.getId();
//row
//tableview variables
con.execute("UPDATE gevonden_bagage SET type = 'data1' WHERE koffer_id = 'c1' ");
//Query
} catch (SQLException ex) {
System.err.println("Error" + ex);
}
}
//get connection, get celldata, get id data from first row, update cell with selected id
full controller class:
package simple;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TablePosition;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
/**
*
* #author admin
*/
public class FXMLUserController extends SimpleController implements Initializable {
#FXML
public TableView<UserDetails> tableview;
#FXML
public TableColumn<UserDetails, String> columnId;
#FXML
public TableColumn<UserDetails, String> columnType;
#FXML
public TableColumn<UserDetails, String> columnKleur;
#FXML
public TableColumn<UserDetails, String> columnLuchthaven;
#FXML
public TableColumn<UserDetails, String> columnKenmerken;
#FXML
public TableColumn<UserDetails, String> columnStatus;
#FXML
public TableColumn<UserDetails, String> columnDatum;
#FXML
private Button btnLoad;
//declare observable list for database data
private ObservableList<UserDetails> data;
private DbConnection dc;
String c1;
#FXML
//strings for getRow method
#Override
public void initialize(URL url, ResourceBundle rb) {
dc = new DbConnection();
loadDataFromDatabase();
}
#FXML
public void loadDataFromDatabase() {
try {
Connection conn = dc.Connect();
data = FXCollections.observableArrayList();
// Execute query and store result in a resultset
ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM gevonden_bagage");
while (rs.next()) {
//get strings
data.add(new UserDetails(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5),
rs.getString(6), rs.getString(7)));
}
} catch (SQLException ex) {
System.err.println("Error" + ex);
}
//Set cell values to tableview.
tableview.setEditable(true);
tableview.getSelectionModel().setCellSelectionEnabled(true);
columnType.setCellFactory(TextFieldTableCell.forTableColumn());
columnKleur.setCellFactory(TextFieldTableCell.forTableColumn());
columnLuchthaven.setCellFactory(TextFieldTableCell.forTableColumn());
columnKenmerken.setCellFactory(TextFieldTableCell.forTableColumn());
columnStatus.setCellFactory(TextFieldTableCell.forTableColumn());
columnDatum.setCellFactory(TextFieldTableCell.forTableColumn());
//makes columns editable
columnId.setCellValueFactory(new PropertyValueFactory<>("id"));
columnType.setCellValueFactory(new PropertyValueFactory<>("type"));
columnKleur.setCellValueFactory(new PropertyValueFactory<>("kleur"));
columnLuchthaven.setCellValueFactory(new PropertyValueFactory<>("luchthaven"));
columnKenmerken.setCellValueFactory(new PropertyValueFactory<>("kenmerken"));
columnStatus.setCellValueFactory(new PropertyValueFactory<>("status"));
columnDatum.setCellValueFactory(new PropertyValueFactory<>("datum"));
columnType.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<UserDetails, String>>() {
#Override
public void handle(TableColumn.CellEditEvent<UserDetails, String> event) {
updataData();
}
});
tableview.setItems(null);
tableview.setItems(data);
}
public void updataData() {
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql://37.128.148.113:3306/FYS", "FYS", "Kcj8g87~");
Statement con = connection.createStatement();
//connection
TablePosition pos = tableview.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
TableColumn col = pos.getTableColumn();
String data1 = (String) col.getCellObservableValue(row).getValue();
//cell
UserDetails row1 = tableview.getSelectionModel().getSelectedItem();
c1 = row1.getId();
//row
//tableview variables
con.execute("UPDATE gevonden_bagage SET type = 'data1' WHERE koffer_id = 'c1' ");
//Query
} catch (SQLException ex) {
System.err.println("Error" + ex);
}
}
//get connection, get celldata, get id data from first row, update cell with selected id
#FXML
public void getRow() {
TablePosition pos = tableview.getSelectionModel().getSelectedCells().get(0);
int row = pos.getRow();
TableColumn col = pos.getTableColumn();
// this gives the value in the selected cell:
String data1 = (String) col.getCellObservableValue(row).getValue();
System.out.println(data1);
//CURRENTLY UNUSED METHOD
}
}
Model class:
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
/**
*
* #author admin
*/
public class UserDetails {
private final StringProperty id;
private final StringProperty type;
private final StringProperty kleur;
private final StringProperty luchthaven;
private final StringProperty kenmerken;
private final StringProperty status;
private final StringProperty datum;
//Default constructor
public UserDetails(String id, String type, String kleur, String luchthaven, String kenmerken, String status, String datum) {
this.id = new SimpleStringProperty(id);
this.type = new SimpleStringProperty(type);
this.kleur = new SimpleStringProperty(kleur);
this.luchthaven = new SimpleStringProperty(luchthaven);
this.kenmerken = new SimpleStringProperty(kenmerken);
this.status = new SimpleStringProperty(status);
this.datum = new SimpleStringProperty(datum);
}
//getters
public String getId() {
return id.get();
}
public String getType() {
return type.get();
}
public String getKleur() {
return kleur.get();
}
public String getLuchthaven() {
return luchthaven.get();
}
public String getKenmerken() {
return kenmerken.get();
}
public String getStatus() {
return status.get();
}
public String getDatum() {
return datum.get();
}
//setters
public void setId(String value) {
id.set(value);
}
public void setType(String value) {
type.set(value);
}
public void setKleur(String value) {
kleur.set(value);
}
public void setLuchthaven(String value) {
luchthaven.set(value);
}
public void setKenmerken(String value) {
kenmerken.set(value);
}
public void setStatus(String value) {
status.set(value);
}
public void setDatum(String value) {
datum.set(value);
}
//property values
public StringProperty idProperty() {
return id;
}
public StringProperty typeProperty() {
return type;
}
public StringProperty kleurProperty() {
return kleur;
}
public StringProperty luchthavenProperty() {
return luchthaven;
}
public StringProperty kenmerkenProperty() {
return kenmerken;
}
public StringProperty statusProperty() {
return status;
}
public StringProperty datumProperty() {
return datum;
}
}
From the TableView documentation:
By default the TableColumn edit commit handler is non-null, with a
default handler that attempts to overwrite the property value for the
item in the currently-being-edited row. It is able to do this as the
Cell.commitEdit(Object) method is passed in the new value, and this is
passed along to the edit commit handler via the CellEditEvent that is
fired. It is simply a matter of calling
TableColumn.CellEditEvent.getNewValue() to retrieve this value.
It is very important to note that if you call
TableColumn.setOnEditCommit(javafx.event.EventHandler) with your own
EventHandler, then you will be removing the default handler. Unless
you then handle the writeback to the property (or the relevant data
source), nothing will happen.
So the problem is that by setting the onEditCommit on columnType, you remove the default handler that actually updates typeProperty in the UserDetails instance. Consequently
String data1 = (String) col.getCellObservableValue(row).getValue();
gives the old value, and your update to the database won't change anything.
Additionally, you have errors in the way you create the SQL statement. You are making the id in the WHERE clause the literal value 'c1' (instead of the value contained in the variable c1, and similarly setting the value of type to the literal value 'data1', instead of the value in the variable data1.
Here is a fix, along with some simplification of the code and some better practices for avoiding SQL injection attacks:
columnType.setOnEditCommit(event -> {
UserDetails user = event.getRowValue();
user.setType(event.getNewValue());
updateData("type", event.getNewValue(), user.getId());
});
and then
private void updateData(String column, String newValue, String id) {
// btw it is way better to keep the connection open while the app is running,
// and just close it when the app shuts down....
// the following "try with resources" at least makes sure things are closed:
try (
Connection connection = DriverManager.getConnection("jdbc:mysql://37.128.148.113:3306/FYS", "FYS", "Kcj8g87~");
PreparedStatement stmt = connection.prepareStatement("UPDATE gevonden_bagage SET "+column+" = ? WHERE koffer_id = ? ");
) {
stmt.setString(1, newValue);
stmt.setString(2, id);
stmt.execute();
} catch (SQLException ex) {
System.err.println("Error");
// if anything goes wrong, you will need the stack trace:
ex.printStackTrace(System.err);
}
}
I have created an entity of a table with name Category and I want to get the data as an entity and display the name of the category into a choicebox using StringConverter.
The problem is when I call addAll() method, it occurs a NullPointerException.
Here is my code:
private ChoiceBox<Category> chbDisplayCustomCategory;
#Override
public void initialize(URL location, ResourceBundle resources) {
emFactory = Persistence.createEntityManagerFactory("Glory_StorePU");
eManager = emFactory.createEntityManager();
Query query = eManager.createNamedQuery("Category.findAll", Category.class);
List<Category> list = query.getResultList();
ObservableList<Category> result=FXCollections.observableArrayList(list);
chbDisplayCustomCategory.getItems().addAll(result);
}
Here is the Entity class (Automatically generated):
package MainPack.Entities;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
#Entity
#Table(name = "CATEGORY")
#XmlRootElement
#NamedQueries({
#NamedQuery(name = "Category.findAll", query = "SELECT c FROM Category c"),
#NamedQuery(name = "Category.findById", query = "SELECT c FROM Category c WHERE c.id = :id"),
#NamedQuery(name = "Category.findByCatName", query = "SELECT c FROM Category c WHERE c.catName = :catName")})
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#Column(name = "ID")
private Long id;
#Basic(optional = false)
#Column(name = "CAT_NAME")
private String catName;
public Category() {
}
public Category(Long id) {
this.id = id;
}
public Category(Long id, String catName) {
this.id = id;
this.catName = catName;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getCatName() {
return catName;
}
public void setCatName(String catName) {
this.catName = catName;
}
#Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
#Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Category)) {
return false;
}
Category other = (Category) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
#Override
public String toString() {
return "MainPack.Entities.Category[ id=" + id + " ]";
}
}
so can you help me please ?
I created an API to delete an Entity by its key however I'm gettin Http 204 and Entity does not delete from data-store.
This is my API,
#ApiMethod(name = "deleteContact", path = "contact", httpMethod = ApiMethod.HttpMethod.DELETE)
public void deleteContact(final #Named("id") long contactId)
{
ofy().delete().type(Contact.class).id(contactId).now();
}
and my Contact class is like this:
#Entity
#Cache
public class Contact
{
#Id
private long id;
#Index
private String cName;
private Email cEmail;
private PhoneNumber cPhoneNumber;
// private key, to connect this Entity to Profile Entity
#Parent
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Key<Profile> profileKey;
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private String profileId;
// default constructor is private
private Contact()
{
}
public Contact(final long id, final String profileId, final ContactForm contactForm)
{
Preconditions.checkNotNull(contactForm.getUserName(), "The name is required");
this.id = id;
this.profileKey = Key.create(Profile.class, profileId);
this.profileId = profileId;
updateWithContactForm(contactForm);
}
/**
* Updates the Contact with ContactForm.
* This method is used upon object creation as well as updating existing Contact.
*
* #param contactForm contains form data sent from the client.
*/
public void updateWithContactForm(final ContactForm contactForm)
{
this.cName = contactForm.getUserName();
this.cEmail = contactForm.getUserEmailAddress();
this.cPhoneNumber = contactForm.getUserPhoneNumber();
}
public long getId() {
return id;
}
public String getcName() {
return cName;
}
public Email getcEmail() {
return cEmail;
}
public PhoneNumber getcPhoneNumber() {
return cPhoneNumber;
}
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
public Key<Profile> getProfileKey() {
return profileKey;
}
// Get a String version of the key
public String getWebSafeKey()
{
return Key.create(profileKey, Contact.class, id).getString();
}
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
public String getProfileId() {
return profileId;
}
#Override
public String toString() {
return "Contact{" +
"id=" + id +
", cName='" + cName + '\'' +
", cEmail=" + cEmail +
", profileId='" + profileId + '\'' +
", cPhoneNumber=" + cPhoneNumber +
'}';
}
}
Any idea would be appreciated.
You've got a parent associated with your class Contact.
// private key, to connect this Entity to Profile Entity
#Parent
#ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Key<Profile> profileKey;
In datastore, Contact entities are stored as:
/User1Profile/SomeContact1
/User1Profile/SomeContact2
Datastore can't search any entity with just the ID of contact (i.e. "SomeContact1") but it can search if you provide parent as well. The right way to delete would be:
ofy().delete().type(Contact.class).parent(profileKey).ids(contactId).now();
Read this for more details: https://code.google.com/p/objectify-appengine/wiki/BasicOperations#Deleting
I have two entities: Dealers and Makes. Dealers have multiple makes.I am able to insert the records as a one-to-many relationship between these two entities.
However, I am not able to retrieve the records based on dealer ID. I tried many different ways and it's still throwing this exception: Property not found make(it is table) of : class name(full classname)
If anyone could provide me with some hints, it would be greatly appreciated.
Dealer:
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.OneToMany;
import javax.persistence.JoinTable;
import javax.persistence.CascadeType;
import javax.persistence.JoinColumn;
#Entity
#Table(name = "dealer")
public class Dealer implements java.io.Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public Dealer(){
}
public Dealer(String DealerName,String dealerPhno,Set makes){
this.dealerName=DealerName;
this.dealerphno=dealerPhno;
this.dealerMakes=makes;
}
public void setDealerMakes(Set<Make> dealerMakes) {
this.dealerMakes = dealerMakes;
}
#Id
#GeneratedValue
#Column(name = "dealer_id")
private long dealerId;
public long getDealerId() {
return dealerId;
}
public void setDealerId(long dealerId) {
this.dealerId = dealerId;
}
public String getDealerName() {
return dealerName;
}
public void setDealerName(String dealerName) {
this.dealerName = dealerName;
}
public String getDealerphno() {
return dealerphno;
}
public void setDealerphno(String dealerphno) {
this.dealerphno = dealerphno;
}
#Column(name = "dealer_name" ,unique=true, nullable = false, length=20)
private String dealerName;
#Column(name = "dealer_phno" , nullable = false, length=20)
private String dealerphno;
#OneToMany(cascade = CascadeType.ALL)
//#OneToMany(fetch=FetchType.EAGER)
#JoinColumn(name="dealer_id", nullable=false)
//#JoinTable(name = "dealer_make", joinColumns = { #JoinColumn(name = "dealer_id") }, inverseJoinColumns = { #JoinColumn(name = "make_id") })
private Set<Make> dealerMakes = new HashSet<Make>(0);
public Set<Make> getDealerMakes() {
return dealerMakes;
}
}
Make
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Entity;
import javax.persistence.Table;
#Entity
#Table(name = "make")
public class Make implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = 2L;
public Make(){
}
public Make(String makeName,String modelName,String price){
this.makeName=makeName;
this.modelName=modelName;
this.price=price;
}
public long getMakeId() {
return makeId;
}
public void setMakeId(long makeId) {
this.makeId = makeId;
}
public String getMakeName() {
return makeName;
}
public void setMakeName(String makeName) {
this.makeName = makeName;
}
public String getModelName() {
return modelName;
}
public void setModelName(String modelName) {
this.modelName = modelName;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
#Id
#GeneratedValue
#Column(name = "make_id")
private long makeId;
#Column(name = "make_name" , nullable = false, length=8)
private String makeName;
#Column(name = "model_name" , nullable = false, length=8)
private String modelName;
#Column(name = "price" , nullable = false, length=8)
private String price;
}
Query I tried:
Criteria dealer=sessionFactory.getCurrentSession().createCriteria(Dealer.class);
Criteria make=dealer.createCriteria(make);
make.add(Restrictions.gt("dealerId",new Long(dealerId)));
List<Dealer> results=dealer.list(); //throwing exception
Ouput i am expecting:
Dealer_Id dealername phonenumber
1 halal 074563485
Make has below records:
make_id makename makeprice dealer_id
1 ford 3000$ 1
2 hyundai 2000$ 1
I want results like dealer details with make details.
OK. So you want to load dealers with their makes, and you only want the dealers having an ID greater than a given ID.
First of all, you could just load dealers. The list of their makes would automatically be loaded by Hibernate when you would call getDealerMakes():
Criteria c = session.createCriteria(Dealer.class);
c.add(Restrictions.gt("dealerId", dealerId)); // let's say delerId = 6
List<Dealer> dealers = c.list(); // execute SQL query select d.* from dealer d where d.dealer_id > 6
for (Dealer dealer : dealers) {
Set<Make> make = dealer.getDealerMakes() // execute SQL query select m.* from make m where m.dealer_id = <theIdOfTheCurrentDealer>
}
This has the disadvantage of executing N + 1 queries.
So if you want to load the dealers and their makes in a single query, you need to set a fetch mode so that the makes are loaded. Note that since the join uses the association that you defined between Dealer and Make, you don't need to tell Hibernate that the ID of the dealer table must be equal to the dealer_id of the make table: Hibernate already knows that thanks to the JoinColumn annotation that you put on the association. The query is thus the following:
Criteria c = session.createCriteria(Dealer.class);
c.add(Restrictions.gt("dealerId", dealerId));
// this tells Hibernate that the makes must be fetched from the database
// you must use the name of the annotated field in the Java class: dealerMakes
c.setFetchMode("dealerMakes", FetchMode.JOIN);
// Hibernate will return instances of Dealer, but it will return the same instance several times
// once per make the dealer has. To avoid this, you must use a distinct root entity transformer
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
List<Delaer> dealers = c.list(); // executes the SQL query:
// select d.*, m.* from dealer d left join make m on d.dealer_id = m.dealer_id where d.dealer_id > 6
for (Dealer dealer : dealers) {
Set<Make> make = dealer.getDealerMakes() // no SQL query. The makes are already loaded
}
criteria.setFetchMode("dealerMakes", FetchMode.JOIN);
criteria.createCriteria("dealerMakes");
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
This do the trick!
public List<Dealer> findDealers() {
Criteria dealer=sessionFactory.getCurrentSession().createCriteria(Dealer.class);
Criteria make=dealer.createCriteria(make);
make.setFetchMode("dealer", FetchMode.JOIN);
make.add(Restrictions.gt("dealerId",new Long(dealerId)));
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); // depends on wat u want
return criteria.list();
}
try to return like this, hope it works, let me know if there is more to it