Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OnetoMany kundera cassandra based on condition #675

Open
sreekanth0041 opened this issue Dec 11, 2014 · 2 comments
Open

OnetoMany kundera cassandra based on condition #675

sreekanth0041 opened this issue Dec 11, 2014 · 2 comments

Comments

@sreekanth0041
Copy link

I have two table one is products and next one is discounts,problem is when i am getting the products i want to get the discounts based on valid date.If date expires then i don't want to get the record.
My code is below
Products.java

@Entity
@Table(name = "products", schema = "db@cassandra")
public class Products {

    @Id
    @Column(name = "prod_id")
    private UUID prod_id;

    @Column(name = "product_name")
    private String store_name;

    @Column(name = "product_price")
    private BigDecimal store_price;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "prod_id")
    private List<Discount> discount;

//setter and getters
}

Discount.java

@Entity
@Table(name = "products", schema = "db@cassandra")
public class Discount{
       @Id
    @Column(name = "discount_id")
    private UUID discount_id;

    @Column(name = "prod_id")
    private UUID prod_id;

    @Column(name = "discount_name")
    private String discount_name;

    @Column(name = "start_date")
    private Timestamp start_date;

    @Column(name = "end_date")
    private Timestamp end_date;

//setter and getters
}

Main.java

          em = emf.createEntityManager();
          Query q = em.createQuery("select p from Products p");
          List<Products> products= q.getResultList();
          em.close();

Can you please help me.

@chhavigangwal
Copy link
Collaborator

@sreekanth0041

In order to enable querying over non-primary key columns you need to create indexes on them in the following manner :

@Entity
@Table(name = "products", schema = "db@cassandra")
@IndexCollection(columns = { @Index(name = "start_date")})
public class Discount{
       @Id
    @Column(name = "discount_id")
    private UUID discount_id;

However, you cannot directly query over associated entity's attribute using Kundera. So you can resolve your problem in the following manner :

  • Fetch all objects of Discount which are not expired
  Query q = em.createQuery("select p from Discount p where p.end_date > :date ").setParameter(new Date());
   List<Discount> discounts= q.getResultList();
  • Loop through discount objects to retrieve the product ids and

Hope that Helps!
Chhavi

@devender-yadav
Copy link
Contributor

@sreekanth0041
Did the above suggestion work for you?

Devender

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants