Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

composite key and Criteria API [Hibernate]

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a composite key with three columns (a,b,c) that I need to map,
I created a @Embeddable PK class

and provided it as @EmbeddedId in the mapping class


I've tried few things but they are not working



(I also tried setting value of C but it didn't help)

Now I want something like SELECT * FROM TABLE_COMPOSITE WHERE A = ? AND B = ? using Criteria API

Please help.
 
prateek urmaliya
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried something like


but I am getting

could not resolve property: a of: TableComposite


am I missing something here ?
 
prateek urmaliya
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it's workin

I tried

final Criteria crit = session.createCriteria(TableComposite.class)
.add(Restrictions.eq("id.A", 123))
.add(Restrictions.eq("id.B", 456));



I was missing that though my field was a the getter was getA
Also I don't need alias as I am not joining anything
Thanks to my colleague DT
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a Class Address with the composite key (personId, departmentId). I created a class PersonDepartmentId as below


To search an address I used criteria


Where personDepartmentId is a reference to an object of type PersonDepartmentId.
Also, please note that the equals method of PersonDepartmentId has been overridden.

And this works....

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alguém pode me explicar o que está errado no código abaixo?

public static void main(String[] args) throws Exception {
SessionFactory session = HibernateUtil.getSessionFactory();
Session sess = session.getCurrentSession();

Transaction tx = sess.beginTransaction();

CompoundKey key1 = new CompoundKey(500, 1001);
Accounts saving = new Accounts();
saving.setCompoundKey(key1);
saving.setAccountBalance(8500);

CompoundKey key2 = new CompoundKey(500, 2001);
Accounts checking = new Accounts();
checking.setCompoundKey(key2);
checking.setAccountBalance(8500);

sess.saveOrUpdate(saving);
sess.saveOrUpdate(checking);

Criteria crit = sess.createCriteria(Accounts.class).add(
Restrictions.eq("compoundKey.userId", new Integer(500)));
List accList = crit.list();
for(int i = 0; i < accList.size(); i++){
Accounts acc = (Accounts) accList.get(i);
System.out.println(acc.getAccountBalance());
}
tx.commit();

session.close();
}
-----------------------------------------------------------------------------------------------
package hibernate;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Accounts {
private CompoundKey compoundKey;
private int accountBalance;

@Id
public CompoundKey getCompoundKey() {
return compoundKey;
}
public void setCompoundKey(CompoundKey compoundKey) {
this.compoundKey = compoundKey;
}
public int getAccountBalance() {
return accountBalance;
}
public void setAccountBalance(int accountBalance) {
this.accountBalance = accountBalance;
}

}
-----------------------------------------------------------------------------------------------
package hibernate;

import java.io.Serializable;

import javax.persistence.Embeddable;
@Embeddable
public class CompoundKey implements Serializable{
private int userId;
private int accountId;

public CompoundKey(int userId, int accountId) {
super();
this.userId = userId;
this.accountId = accountId;
}

public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public int getAccountId() {
return accountId;
}
public void setAccountId(int accountId) {
this.accountId = accountId;
}

public CompoundKey() {
super();
}
}
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic