I have two tables CatalogueBase and CatalogueCopydetails now i am using Hibernate search for CatalogueBase table but i wanted to search even in CatalogueCopydetails table. This two tables are related with #ManyToOne (i.e CatalogueCopydetails using CatalogueBase id as foreign key), hear for one entry of CatalogueBase their will be 'n' numbers of CatalogueCopydetails
CatalogueBase POJO Class
#Indexed
#JsonAutoDetect
#Entity
#Table(name="catalogueBase")
public class CatalogueBase extends BaseObject implements Serializable {
private Long id;
......
#Id
#GeneratedValue
#Column(name="id")
#Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
....
CatalogueCopydetails POJO Class
#JsonAutoDetect
#Entity
#Table(name="cataloguecopydetails")
public class CatalogueCopyDetails extends BaseObject implements Serializable {
private CatalogueBase catalogueBase;
......
#ManyToOne
#JoinColumn(name="cataloguebaseid" , insertable=true, updatable=true,nullable=true)
public CatalogueBase getCatalogueBase() {
return catalogueBase;
}
public void setCatalogueBase(CatalogueBase catalogueBase) {
this.catalogueBase = catalogueBase;
}
......
at least how can i use #IndexedEmbedded for this scenario (i don't think i can use #IndexedEmbedded because CatalogueBase have no relation to CatalogueCopyDetails like OneToOne or OneToMany etc only CatalogueCopyDetails references CatalogueBase )
how can i do this..?, any help will be appreciated, Thanks.
The easiest way would of course be to make the relation bidirectional. Is there a good reason why you don't want to do that? The other thing you could do is to add #Indexed to CatalogueCopyDetails as well and use #IndexedEmbedded on CatalogueBase. You could then write a query using the CatalogueCopyDetails index. Whether this works will depend on your use case and what you actually want as result of query.
Related
THis is the main entity class, which is having an embeddedId
public class LabResHivMutation implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private LabResHivMutationPK id;
private String comments;
#Column(name="MUTATION_TYPE_ID")
private BigDecimal mutationTypeId;
#Column(name="VALUE")
private String value;
}
This is the embeddable key
#Embeddable
public class LabResHivMutationPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
#Column(name="TO_INST")
private Long toInst;
#Column(name="REL_INVSTID")
private long relInvstid;
#Column(name="MUTATION_ID")
private long mutationId;
}
Is there any delete methos available in spring data Jpa to delete based on only two of the embaddable key(toInst,relInvstid).
I still can write a JPQL query to delete it. My question is there any method available for this.
like deleteById ?
Yes there is, repo.deleteByIdToInstAndIdRelInvstid(toInst,relInnvstid)
As you see you have to specify deleteByIdToInst , this is how you reference a field of an embedded ID , the same as you would reference a field of a foreign relation. Here Id matches your field naming
#EmbeddedId
private LabResHivMutationPK id;
There are two ways to delete an entity: either using its own "JPA Repository derived delete method" long deleteByFirstIdAndSecondId(long firstId , secondId)
In your service you can simply call it : repository.deleteByFirstIdAndSecondId(long firstId , secondId)
Another way is through the parent entity by excluding the child entity (or entities depends on the relation type).
User underscore '_' when entity will have multiple keys with using #Embedded keys.
example :repository.deleteByid_toInst();
I m working with spring boot, i have these two classes
#Entity
#Table(name="products")
public class Product implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idProduit;
//other attributes..
#ManyToOne
#JoinColumn(name="idCategory")
private Category category;
and category class :
#Entity
public class Category implements Serializable {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Long idcategory;
//attributes...
#OneToMany(mappedBy="category")
private Collection<Product> products;
i want to code a methode to save product
public long saveProduct(Product p, Long idCat)
Is there a method defined in JpaRepository which can do this or, should i add a service Layer and define my method like below or define it a custom method in Repository ?
public long saveProduct(Product p, Long idCat){
Category c=getCategory(idCat);
p.setCategory(c);
em.persist(p);
return p.getIdProduct();
}
I think you should add a service Layer and define a transactional method in order to handle exceptions like CategoryNotFoundException (when Category c=getCategory(idCat) fires one) ,
DataIntegrityViolationException....
Building a solution without a service Layer isn't a good practice, since you will have to handle transactions and propagations manually and you will risk having dirty reads.
We have the following situation: an entity and a pojo in Objectify v5.
As you can see, the entityclass has a reference to the pojo, like this:
#Entity
public class TestCustomer {
#Id
public Long id;
TestIbj ibj;
}
class TestIbj {
TestCustomer customer;
}
This fails with a StackOverflowError when we try to save it like this:
TestCustomer testCustomer = new TestCustomer();
OfyService.ofy().save().entity(testCustomer).now();
Error:
java.lang.StackOverflowError
at java.lang.Class.getMethod0(Class.java:2772)
at java.lang.Class.isCheckMemberAccessOverridden(Class.java:2214)
at java.lang.Class.checkMemberAccess(Class.java:2233)
at java.lang.Class.getDeclaredMethods(Class.java:1854)
at...
Our question is: Why is this not allowed/failing? If we change the pojo into an Entity, it works fine, but we don't understand why that is..
Thank you very much!
StackoverflowError almost always means that there is an unbounded recursion. It's the case in your code as well:
#Entity
public class TestCustomer {
#Id
public Long id;
TestIbj ibj; // TestCustomer contains TestIbj
}
class TestIbj {
TestCustomer customer; // TestIbj contains TestCustomer (which further contains TestIbj.. and so on)
}
Due to this circular dependency (shown in code above), objectify will never be able to construct object graph.
I need to set some private fields in an object using another object's fields. Those two objects may not be instances of same class.
What I see from a short reading, I can use Apache's BeanUtils and Spring's ReflectionUtils for that. I couldn't find a satisfying explanation for them regarding security, performance, support etc.
The solution will be used in production environment too, so I need a concrete solution.
Which approach do you suggest for such a task.
I think you need use just the BeanUtils library. See my sample, i do a copy properties from CustomerBean to SellerBean.
package testes.beanutils;
import org.apache.commons.beanutils.BeanUtils;
public class Main {
public static void main(String[] args) throws Exception {
Customer customer = new Customer();
customer.setId((long)1);
customer.setName("Bruno");
customer.setLastname("Tafarelo");
Seller seller = new Seller();
BeanUtils.copyProperties(seller, customer);
System.out.println(customer);
System.out.println(seller);
}
}
class Customer {
private Long id;
private String name;
private String lastname;
//getters and setters
//toString
}
class Seller {
private Long id;
private String name;
private int sales;
//getters and setters
//toString
}
In my model, I have a [Customer] table with a self-assocation as:
1 [Customer] may have 1 [Sponsor], and Sponsors are Customers.
In EF 6, I want to know if it's possible to split the entity Customer in two separate entities [Customer] and [Sponsor] and assign them the same table?
Thanks
You can inherit entities to make an alias consider this example:
public class Person
{
public int ID {get;set;}
// your shared properties here
}
public class Customer: Person
{
// additional properties such as
public virtual Sponsor Sponsor {get;set;}
}
public class Sponsor : Person
{
// this could be have own properties or not
}
Now your DbContext could be something like this:
public class MyDbContext:DbContext
{
public IDbSet<Person> Persons{get;set;}
// optionally you could add child objects
public IDbSet<Customer> Customers{get;set;}
public IDbSet<Sponsor> Sponsors{get;set;}
}
Because of using inheritance EF just create one table for your Entities. But you have 3 separate class.
yes you can
public class Customer
{
public int ID {get;set;}
//all your columns goes here
///these 2 lines for foreign key mapping except sponsor
public virtual Customer Sponsor {get;set;}
public virtual IList<Customer> CustomerList{get;set;}
}
in your DbContext
public DbSet<Customer> Customers{get;set;}