I am doing a one to one join to fetch result using annotation. I have two classes the first one is T_ARM_Details and the second class is ARM_Ticket_Details and T_ARM_Alert_Type. I do a one to one join using annotation but the join is not happening and is fetching data from a single table
package com.cts.met.bo;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
#Entity
#Table(name="T_ARM_Details")
public class ARM_Ticket_Details implements Serializable {
#Id
#Column(name="ticket_id")
String Ticket_id;
#Column(name="arm_alert_type_id")
int arm_alert_type_id;
/*#ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
#JoinTable(name="T_arm_alert_type",joinColumns=#JoinColumn(name="arm_alert_type_id",referencedColumnName="arm_alert_type_id"),inverseJoinColumns=#JoinColumn(name= "response_status_id"))*/
public int getArm_alert_type_id() {
return arm_alert_type_id;
}
public void setArm_alert_type_id(int arm_alert_type_id) {
this.arm_alert_type_id = arm_alert_type_id;
}
private T_ARM_Alert_Type t_arm_alert_type;
//private T_SLA_Status t_sla_statuse;
public String getTicket_id() {
return Ticket_id;
}
public void setTicket_id(String ticket_id) {
Ticket_id = ticket_id;
}
#OneToOne(fetch=FetchType.LAZY,mappedBy="ARM_Ticket_Details",
cascade=CascadeType.ALL)
#JoinTable(name="T_arm_alert_type",
joinColumns=#JoinColumn(name="arm_alert_type_id"))//,
referencedColumnName="arm_alert_type_id"))
//#JoinColumn(name="arm_alert_type_id")
public T_ARM_Alert_Type getT_arm_alert_type() {
return t_arm_alert_type;
}
public void setT_arm_alert_type(T_ARM_Alert_Type t_arm_alert_type) {
this.t_arm_alert_type = t_arm_alert_type;
}
/*#ManyToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
#JoinTable(name="T_sla_status",
joinColumns=#JoinColumn(name="resolution_status_id")//,
//inverseJoinColumns=#JoinColumn(name= "response_status_id")
)
//#JoinColumn(name="status_id")
*/
/*public T_SLA_Status getT_sla_statuse() {
return t_sla_statuse;
}
public void setT_sla_statuse(T_SLA_Status t_sla_statuse) {
this.t_sla_statuse = t_sla_statuse;
}*/
}
And the next one is as follows:
package com.cts.met.bo;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
#SuppressWarnings("serial")
#Entity
#Table(name="T_arm_alert_type")
public class T_ARM_Alert_Type implements Serializable{
#Id
//#OneToOne(targetEntity=ARM_Ticket_Details.class,cascade=CascadeType.ALL)
//#JoinColumn(name="arm_alert_type_id",referencedColumnName="arm_alert_type_
id")
/*#OneToOne(fetch=FetchType.LAZY)
#PrimaryKeyJoinColumn*/
//#JoinColumn(name="arm_alert_type_id")
int arm_alert_type_id;
#Column(name="arm_alert_desc")
String arm_alert_desc;
#OneToOne(fetch=FetchType.LAZY)
#JoinColumn(name="ticket_id")
private ARM_Ticket_Details arm;
public ARM_Ticket_Details getArm() {
return arm;
}
public void setArm(ARM_Ticket_Details arm) {
this.arm = arm;
}
public int getArm_alert_type_id() {
return arm_alert_type_id;
}
public void setArm_alert_type_id(int arm_alert_type_id) {
this.arm_alert_type_id = arm_alert_type_id;
}
public String getArm_alert_desc() {
return arm_alert_desc;
}
public void setArm_alert_desc(String arm_alert_desc) {
this.arm_alert_desc = arm_alert_desc;
}
}
My Controller looks like this:
#RequestMapping(value = "/editTicket", method = RequestMethod.GET)
public ModelAndView editTicket(HttpServletRequest request){
String TicketId = request.getParameter("id");
ARM_Ticket_Details ticket = pmportalService.getARMTicketById(TicketId);
ModelAndView model = new ModelAndView("editTicket");
model.addObject("ticket", ticket);
return model;
}
I expect a join a to happen here but it is giving the below error:
select
arm_ticket0_.ticket_id as ticket_i1_1_0_,
arm_ticket0_.arm_alert_type_id as arm_aler2_1_0_,
arm_ticket0_.t_arm_alert_type as t_arm_al3_1_0_
from
T_ARM_Details arm_ticket0_
where
arm_ticket0_.ticket_id=?
Can anyone please help? Btw I am new to hibernate.
I think your problem is that you are using Lazy FetchType, where you should use EAGER instead, or skip defining it explicitly, as it is the default FetchType value. Lazy loading is used for performance reasons, most commonly when you have a OneToMany relationship and loading too many objects, if they are not really needed affects performance on each operations. You rarely need to use lazy fetching for OneToOne relationship, because you are only using one additional object and that won't affect the performance of your application, except maybe in special cases of enormous objects. You can read more about different fetch types and their effect in the documentation.
Related
I am getting:
java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap
in my Spring Boot Project in which I am trying to persist data of two Pojo classes using Hibernate One-To-Many Relationship. I am trying to save values of a persistent class that has a Collection element Set defined.
The Parent Pojo Class:-
package com.example.demo.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
#Entity
#Table(name = "vendor")
public class Vendor {
#Id
int vendorId;
#Column
String vendorName;
#OneToMany(fetch = FetchType.LAZY, targetEntity = Customer.class, cascade = CascadeType.ALL)
#JoinColumn(name = "vendorId")
Set children;
public int getVendorId() {
return vendorId;
}
public void setVendorId(int vendorId) {
this.vendorId = vendorId;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String vendorName) {
this.vendorName = vendorName;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
}
Child Pojo Class:-
package com.example.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GeneratorType;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
#Entity
#Table(name = "customer")
public class Customer {
#Id
int customerId;
#Column
String customerName;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
Controller Class:-
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Vendor;
import com.example.demo.service.VendorDataSaveService;
#RestController
public class VendorSaveController {
#Autowired
private VendorDataSaveService dataSaveService;
#PostMapping("/save")
public void saveVendor(#RequestBody Vendor vendor) {
dataSaveService.saveVendorRecord(vendor);
}
}
Service Class:-
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.model.Vendor;
import com.example.demo.repository.VendorDataSaveRepository;
#Service
public class VendorDataSaveService {
#Autowired
private VendorDataSaveRepository repository;
public void saveVendorRecord(Vendor vendor) {
repository.save(vendor);
}
}
Repository Class:-
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.Vendor;
public interface VendorDataSaveRepository extends JpaRepository<Vendor, Integer> {
}
The JSON Format which I am sending from Postman:-
{
"vendorId" : 101,
"vendorName" : "JAIN BOOKS",
"children" : [{
"customerId" : 1,
"customerName" : "AMIT"
}]
}
Error Message From Console:-
java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap
Error Message On Postman:-
Error accessing field [int com.example.demo.model.Customer.customerId] by reflection for persistent property [com.example.demo.model.Customer#customerId] : {customerId=1, customerName=AMIT}; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [int com.example.demo.model.Customer.customerId] by reflection for persistent property [com.example.demo.model.Customer#customerId] : {customerId=1, customerName=AMIT}"
Need To add Generic type Customer argument in Set children Collection type.
Set<Customer> children;
I have a problem when I POST from a single REST call to create a Library and associate Books for the library. The library record is created, but the associated books are not. Library and Book has oneToMany relationship.
My POST request and response is as below -
POST - http://localhost:8080/libraries/
REQUEST BODY
{
"name":"My Library",
"books": [
{"title": "Effective Java", "isbn": "1234"},
{"title": "Head First Java", "isbn": "5678"}
]
}
REPOSNSE
1
GET Libraries after POST - http://localhost:8080/libraries/
[
{
"id": 1,
"name": "My Library",
"books": [],
"address": null
}
]
POST to create Library and add Books
GET REQUEST for Libraries
MODELs
package com.publiclibrary.domain;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import org.springframework.data.rest.core.annotation.RestResource;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
#Data
#NoArgsConstructor
#Entity
public class Library {
#Id
#GeneratedValue
private long id;
#Column
private String name;
#OneToMany(mappedBy = "library")
private List<Book> books;
#OneToOne
#JoinColumn(name = "address_id")
#RestResource(path = "libraryAddress", rel="address")
private Address address;
// standard constructor, getters, setters
public Library(String name) {
super();
this.name = name;
}
}
package com.publiclibrary.domain;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
#Data
#NoArgsConstructor
//#Builder
//#AllArgsConstructor
#Entity
public class Book {
#Id
#GeneratedValue
private long id;
#NotNull
private String title, isbn;
#ManyToOne
#JoinColumn(name="library_id")
private Library library;
#ManyToMany(mappedBy = "books")
private List<Author> authors;
}
REPOSITORY
package com.publiclibrary.repo;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import com.publiclibrary.domain.Book;
#RepositoryRestResource(path = "books", collectionResourceRel = "books")
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
}
package com.publiclibrary.repo;
import org.springframework.data.repository.CrudRepository;
import com.publiclibrary.domain.Library;
public interface LibraryRepository extends CrudRepository<Library, Long> {
}
Service
package com.publiclibrary.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.publiclibrary.domain.Library;
import com.publiclibrary.repo.LibraryRepository;
#Service
public class LibraryService {
#Autowired
LibraryRepository libraryRepository;
public List<Library> getAllLibrarys() {
List<Library> librarys = new ArrayList<Library>();
libraryRepository.findAll().forEach(library -> librarys.add(library));
return librarys;
}
public Library getLibraryById(long id) {
return libraryRepository.findById(id).get();
}
public void saveOrUpdate(Library library) {
libraryRepository.save(library);
}
public void delete(long id) {
libraryRepository.deleteById(id);
}
}
RESTCONTROLLER
package com.publiclibrary.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.publiclibrary.domain.Library;
import com.publiclibrary.service.LibraryService;
#RestController
public class LibraryController {
#Autowired
LibraryService libraryService;
#GetMapping("/libraries")
private List<Library> getAllLibrarys() {
return libraryService.getAllLibrarys();
}
#GetMapping("/libraries/{id}")
private Library getLibrary(#PathVariable("id") int id) {
return libraryService.getLibraryById(id);
}
#DeleteMapping("/libraries/{id}")
private void deleteLibrary(#PathVariable("id") int id) {
libraryService.delete(id);
}
#PostMapping("/libraries")
private long saveLibrary(#RequestBody Library library) {
libraryService.saveOrUpdate(library);
return library.getId();
}
}
How can I create a Library and add Books as I intend? Appreciate any help!
Try adding cascade persist (or better just cascade all) on books collection in Library class. E.g.
#OneToMany(fetch = FetchType.LAZY, mappedBy = "library", cascade = CascadeType.ALL)
private List<Book> books;
I followed this article and resolved the problem. I explicitly handled parsing the JSON and creating my data objects. Additionally, I added add and remove methods in parent (Library) class and defined the equals and hashcode for reasons explained in the link above.
My code changes as below -
Library -
#OneToMany(mappedBy = "library", cascade = CascadeType.ALL, orphanRemoval = true)
#JsonIgnoreProperties("library")
private List<Book> books = new ArrayList<>();
public void addBook(Book book) {
books.add(book);
book.setLibrary(this);
}
public void removeBook(Book book) {
books.remove(book);
book.setLibrary(null);
}
Book -
#JsonIgnoreProperties("books")
private Library library;
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Book )) return false;
return id != null && id.equals(((Book) o).id);
}
#Override
public int hashCode() {
return 31;
}
LibraryController -
#PostMapping("/libraries")
private long saveLibrary(#RequestBody Map<String, Object> payload) {
Library library = new Library();
library.setName(payload.get("name").toString());
#SuppressWarnings("unchecked")
List<Map<String, Object>> books = (List<Map<String, Object>>) payload.get("books");
for (Map<String, Object> bookObj : books) {
Book book = new Book();
book.setTitle(bookObj.get("title").toString());
book.setIsbn(bookObj.get("isbn").toString());
library.addBook(book);
}
libraryService.saveOrUpdate(library);
return library.getId();
}
Been having ridiculous trouble with using the AND in the CrudReposity. All I want to do it find if two things are Not Null and display them.
public interface StudentRepository extends CrudRepository<Student, Integer>{
List<Student> findByItemAIsNotNullAndItemBIsNotNull();
}
When I run this, it seems to be running the AND as an OR (I tried both), so it's showing things that all null in one of them.
Any Help will be appreciated
You Code is correct may be problem in other section. Otherwise you can see this code. It may help you. Here I skipped service layer though its for only test.
package com.example.stack;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Data{
#Id
#GeneratedValue
Integer id;
String itemA;
String itemB;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getItemA() {
return itemA;
}
public void setItemA(String itemA) {
this.itemA = itemA;
}
public String getItemB() {
return itemB;
}
public void setItemB(String itemB) {
this.itemB = itemB;
}
}
Repository Class
package com.example.stack;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
public interface TestRepository extends CrudRepository<Data, Integer>{
List<Data> findByItemAIsNotNullAndItemBIsNotNull();
}
Controller CLass
package com.example.stack;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
#RestController
#RequestMapping(value = "/test")
public class TestController {
#Autowired
TestRepository repo;
#GetMapping
public List<Data> getTest()
{
return (List<Data>) repo.findByItemAIsNotNullAndItemBIsNotNull();
}
}
Database :
Response :
I have many to many mapping in hibernate with join table and have 3 different classes. OperationEntity and EndPointEntity has manyToMany mapping. I have tried using #Cascade(org.hibernate.annotations.CascadeType.ALL) and #ManyToOne(cascade=CascadeType.ALL) annotations. But when checked in Database, on delete and on update are RESTRICT. Below is the code:
OperationEntity.java
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
#Entity
#Table(name="t_operation", schema="test")
public class OperationEntity implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#Column(name="op_id")
#GeneratedValue(strategy=GenerationType.AUTO)
private int opId;
#Column(name="op_name")
private String opName;
#Column(name="op_desc")
private String opDesc;
#OneToMany(mappedBy="operationsEntity")
private List<OpEndPointEntity> listOfOperationsEndpoints;
public int getOpId() {
return opId;
}
public void setOpId(int opId) {
this.opId = opId;
}
public String getOpName() {
return opName;
}
public void setOpName(String opName) {
this.opName = opName;
}
public String getOpDesc() {
return opDesc;
}
public void setOpDesc(String opDesc) {
this.opDesc = opDesc;
}
public List<OpEndPointEntity> getListOfOperationsEndpoints() {
return listOfOperationsEndpoints;
}
public void setListOfOperationsEndpoints(
List<OpEndPointEntity> listOfOperationsEndpoints) {
this.listOfOperationsEndpoints = listOfOperationsEndpoints;
}
}
EndPointEntity.java
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
#Entity
#Table(name="t_endPoint", schema="test")
public class EndPointEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name="end_point_id")
#GeneratedValue(strategy=GenerationType.AUTO)
private int endPointId;
#Column(name="end_point")
private String endPoint;
#Column(name="end_point_desc")
private String endPointDesc;
#OneToMany(mappedBy="endPointEntity")
private List<OpEndPointEntity> listOpEndPoint;
public int getEndPointId() {
return endPointId;
}
public void setEndPointId(int endPointId) {
this.endPointId = endPointId;
}
public String getEndPoint() {
return endPoint;
}
public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}
public String getEndPointDesc() {
return endPointDesc;
}
public void setEndPointDesc(String endPointDesc) {
this.endPointDesc = endPointDesc;
}
public List<OpEndPointEntity> getListOpEndPoint() {
return listOpEndPoint;
}
public void setListOpEndPoint(List<OpEndPointEntity> listOpEndPoint) {
this.listOpEndPoint = listOpEndPoint;
}
}
Mapping class : OpEndPointEntity.java
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
#Entity
#Table(name="t_op_endpoint_map", schema="test")
public class OpEndPointEntity implements Serializable {
private static final long serialVersionUID = 71L;
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="OP_ENDPOINT_ID")
private Integer operationEndpointId;
#ManyToOne(cascade=CascadeType.ALL)
#Cascade(org.hibernate.annotations.CascadeType.ALL)
#JoinColumn(name="end_point_id")
private EndPointEntity endPointEntity;
#ManyToOne
#Cascade(org.hibernate.annotations.CascadeType.ALL)
#JoinColumn(name="op_id")
private OperationEntity operationsEntity;
public Integer getOperationEndpointId() {
return operationEndpointId;
}
public void setOperationEndpointId(Integer operationEndpointId) {
this.operationEndpointId = operationEndpointId;
}
public EndPointEntity getEndPointEntity() {
return endPointEntity;
}
public void setEndPointEntity(EndPointEntity endPointEntity) {
this.endPointEntity = endPointEntity;
}
public OperationEntity getOperationsEntity() {
return operationsEntity;
}
public void setOperationsEntity(OperationEntity operationsEntity) {
this.operationsEntity = operationsEntity;
}
}
Please provide a way to make on delete and on update CASCADE. Can it be jar issue?
Did you delete it from Java code or using terminal? If you delete it from terminal, it won't work. Once you backup with mysqldump, you will see there's no ON DELETE SET NULL or ON DELETE CASCADE in that sql file. That means it works only with Java Code. Try deleting it from Java Code. I'm not sure how you delete it. And I need to see both classes to see the code.
I'm using Play!Framwork 1.2.5.
I'm trying to implement the CRUD in my project for my casses in models. I followed the steps described here, but I'm getting the following error:
Model models.Media is not managed by any plugin
at play.db.Model$Manager.factoryFor(Model.java:57)
at controllers.CRUD$ObjectType.<init>(CRUD.java:215)
at controllers.CRUD$ObjectType.get(CRUD.java:238)
at controllers.CRUD$ObjectType$get.call(Unknown Source)
at {module:crud}/app/views/tags/crud/types.tag.(line:4)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:247)
at play.templates.GroovyTemplate$ExecutableTemplate.invokeTag(GroovyTemplate.java:401)
at {module:crud}/conf/routes.(line:4)
at play.templates.GroovyTemplate.internalRender(GroovyTemplate.java:247)
at play.templates.Template.render(Template.java:26)
at play.templates.GroovyTemplate.render(GroovyTemplate.java:202)
at play.mvc.Router.parse(Router.java:162)
at play.mvc.Router.parse(Router.java:190)
at play.mvc.Router.parse(Router.java:164)
at play.mvc.Router.load(Router.java:48)
at play.mvc.Router.detectChanges(Router.java:219)
at Invocation.HTTP Request(Play!)
here is the code of the class media:
import org.hibernate.annotations.Entity;
import org.hibernate.annotations.GenericGenerator;
import play.db.jpa.Model;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
#Entity
public class Media extends Model{
#Id
private String uuid;
#OneToOne
private Video video;
#OneToOne
private Picture picture;
public String getUuid() {
return uuid;
}
public void setUuid(String id) {
this.uuid = id;
}
public Video getVideo() {
return video;
}
public void setVideo(Video video) {
this.video = video;
}
public Picture getPicture() {
return picture;
}
public void setPicture(Picture picture) {
this.picture = picture;
}
#Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Media))
return false;
Media media = (Media) obj;
return media.getUuid().equals(this.uuid);
}
}
I think you are not used the right import statement.
Your import on Media.java model should be like below:
Media Model
import play.db.jpa.Model;
// should import javax.persistence.* package rather than org.hibernate.*
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
#Entity
public class Media extends Model {
....
}
If you marked field with relationship annotation (like #OneToOne), you should define the object as an Entity. So, your other model such as Picture.java or Video.java should import the same kind with Media.java like below:
Picture Model
import play.db.jpa.Model;
// don't forget to import javax.persistence.* package
import javax.persistence.Entity;
#Entity
public class Picture extends Model {
...
}
Video Model
import play.db.jpa.Model;
// don't forget to import javax.persistence.* package too
import javax.persistence.Entity;
#Entity
public class Video extends Model {
...
}
I have tried this, and it works with me. Hope it helps to solve your problem.
Regards.