Ref.get returning null - java

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.

Related

Is it possible to communicate between entities in different backend projects in intelliJ idea Maven (Springboot/Hibernate)?

I am wondering if I could communicate between entities in different backend projects in intelliJ idea (Maven/Springboot).
I have a project in which the entity 'Pakketje' can be found. And I also have a project in which the entity 'Bedrijf' can be found.
I would like to have a #oneToMany relationship between 'Bedrijf' entity and 'Pakketje' entity.
So one 'Bedrijf' has a List of 'Pakketjes'.
This is the entity from project number 1, 'Pakketje':
#Entity
public class Pakketje {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private int code;
private String status = "In magazijn";
public Pakketje() {
}
public Pakketje(int Id, int Code) {
this.id = Id;
this.code = Code;
this.status = "In magazijn";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
This is the entity from project number 2, 'Bedrijf':
#Entity
public class Bedrijf {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String bedrijfsnaam;
private String gebruikersnaam;
private String wachtwoord;
private List<Pakketje> pakketjes;
public Bedrijf() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBedrijfsnaam() {
return bedrijfsnaam;
}
public void setBedrijfsnaam(String bedrijfsnaam) {
this.bedrijfsnaam = bedrijfsnaam;
}
public String getGebruikersnaam() {
return gebruikersnaam;
}
public void setGebruikersnaam(String gebruikersnaam) {
this.gebruikersnaam = gebruikersnaam;
}
public String getWachtwoord() {
return wachtwoord;
}
public void setWachtwoord(String wachtwoord) {
this.wachtwoord = wachtwoord;
}
}
Unfortunately the 'Bedrijf' entity does not recognize the 'Pakketje' entity because they are in 2 separate projects.
Can this be resolved? or what could be an alternative? School tells me I MUST have 2 backend projects communicating with each other.

SonarQube Major Issue: Duplicated blocks of code must be removed same class with different annotations

Sonarqube block my build due to Duplicated blocks for this two classes :
#Entity
#Table(name = "my_table")
public class Employee {
#Id
#Column(name = "ID")
Integer id;
#Column(name = "NAME")
String name;
#Column(name = "AGE")
Integer age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age= age;
}
public int getAge() {
return age;
}
public void setId(int id) {
this.id= id;
}
public int getId() {
return id;
}
}
#ApiModel(value = "Employee")
public class EmployeeDTO {
#ApiModelProperty(required = false, example = "1")
Integer id;
#ApiModelProperty(required = false, example = "Jhon")
String name;
#ApiModelProperty(required = false, example = "25")
Integer age;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setAge(int age) {
this.age= age;
}
public int getAge() {
return age;
}
public void setId(int id) {
this.id= id;
}
public int getId() {
return id;
}
}
any idea how i can resolve this issue since i don't want to create an abstract class then inherit from it because i will lose the swagger and JPA annotations and i want to keep the visibility for each class and layer.
thanks.
Unfortunately, the only real resolution is to set a duplications exclusion for those two classes (assuming this is 1 class/file).
Go to Project Settings -> General Settings -> Analysis Scope -> C. Duplication Exclusions

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.

hibernate many to one foreign key not working

the follwing is my hibernate example for one to many relationship
cart java class
#Entity
#Table(name="cart")
public class Solocart {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="carts_id")
int id;
#Column(name="cust_name")
String name;
#OneToMany(mappedBy="cartitem")
Set<Soloitems>soloitem;
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 Set<Soloitems> getSoloitem() {
return soloitem;
}
public void setSoloitem(Set<Soloitems> soloitem) {
this.soloitem = soloitem;
}
}
next items java file
#Entity
#Table(name="cartitem")
public class Soloitems {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="cart_id")
private int id;
#Column(name="no_item")
private int number;
#ManyToOne
#JoinColumn(name="carts_id")
private Solocart cartitem;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public Solocart getCartitem() {
return cartitem;
}
public void setCartitem(Solocart cartitem) {
this.cartitem = cartitem;
}
impl code
Session sn=Util.getSessionFactory().openSession();
sn.beginTransaction();
Solocart crt=new Solocart();
crt.setName("solomon");
Soloitems itm1=new Soloitems();
Soloitems itm2=new Soloitems();
itm1.setNumber(5);
itm2.setNumber(8);
Set<Soloitems>values= new HashSet<Soloitems>();
values.add(itm1);
values.add(itm2);
crt.setSoloitem(values);
sn.save(crt);
sn.save(itm2);
sn.save(itm1);
sn.getTransaction().commit();
sn.close();
System.out.println("sucessfully created");
here one cart should have many items while running both the tanles were updated but
# cart_id, no_item, carts_id
'1', ' 8', NULL
'2', ' 5', NULL
second table
# carts_id, cust_name
'1', ' solomon'
as you see both the tables has been updated but the foreignkey herein this case carts_id didnt get updated in the owner class i have used joincolumn
You have a bi-directional relationship between entities Solocart and Soloitems so in your code you need to maintain the relationship from both sides of entities.
So based on this, in your code you are just setting the Soloitems to Solocart but you missed to set the Solocart to Soloitems, so as mentioned by Predrag add below lines of code to maintain the relationship:
itm1.setCartitem(crt);
itm2.setCartitem(crt);
You are not setting Solocart anywhere to your Soloitems. Try adding this to your code
itm1.setCartitem(crt);
itm2.setCartitem(crt);

JPA Many to Many cascade problem

If I create a Customer and Controller, then associate my Controller with a customer it saves fine.
If I then remove my controller it doesn't remove the relationship between them.
This causes an EntityNotFoundException when I load the Customer.
javax.persistence.EntityNotFoundException: Unable to find Controller with id 22
I'd like to know how to map this so that when a Controller is deleted the relationship is also deleted.
Database Tables
customer
controller
customer_controllers - mapping table.
The Controller's id is not getting removed from the customer_controllers mapping table.
#Entity
public class Customer implements Serializable{
private Integer id;
private Set<Controller> controllers;
#Id
#GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
#ManyToMany(cascade={CascadeType.ALL})
public Set<Controller> getControllers()
{
return controllers;
}
public void setControllers(Set<Controller> controllers)
{
this.controllers = controllers;
}
}
#Entity
public class Controller implements Serializable{
private Integer id;
private String name;
private String abbreviation;
#Id
#GeneratedValue
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAbbreviation()
{
return abbreviation;
}
public void setAbbreviation(String abbreviation)
{
this.abbreviation = abbreviation;
}
}
If you have a ManyToMany then you should map Controller to Customer with a
#ManyToMany(mappedBy="controllers")
or the other way around, depending on which side is the owning side.
As you have it now the relation is not fully defined and it will fail on events like "Cascade".
Have you checked the javadoc for #ManyToMany?
It includes the above example mappings.
you need to make the relationship bidirectional, so that the controller object is aware of its relationship to the customer. Yhis means that when the controller is deleted the record in the join table is also deleted.
This isn't the exact mapping but it gives you the idea.
#Entity
public class Controller implements Serializable{
private Integer id;
private String name;
private String abbreviation;
private Set<Customer> customers;
#Id
#GeneratedValue
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAbbreviation()
{
return abbreviation;
}
public void setAbbreviation(String abbreviation)
{
this.abbreviation = abbreviation;
}
#ManyToMany(cascade={CascadeType.ALL})
public Set<Customer> getCustomers()
{
return customers;
}
public void setCustomers(Set<Customers> customers)
{
this.customers= customers;
}
}

Categories