Hi,
Im having trouble with composite key and OpenJPA.
I was using hibernate so excuse my lack of OpenJPA knowledge.
I have a table already created and one that I cannot modify :
CREATE TABLE `admin_skills` (
`admin_id` INT(11) NOT NULL,
`account_id` INT(11) NOT NULL,
`skill` INT(11) NOT NULL DEFAULT '5',
UNIQUE INDEX `account_id` (`account_id`, `admin_id`),
INDEX `admin_id` (`admin_id`),
CONSTRAINT `admin_skills_ibfk_1` FOREIGN KEY (`admin_id`) REFERENCES
`admins` (`idadmin`) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT `admin_skills_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES
`account` (`account_id`) ON UPDATE CASCADE ON DELETE CASCADE
)
I have created following entity/key :
@Entity
@Table(name = "admin_skills")
public class DitaAdminSkill {
@EmbeddedId
private DitaAdminSkillPK id;
@Column(name = "skill")
private Integer skill;
//getters & setters
@Embeddable
public class DitaAdminSkillPK implements Serializable{
@ManyToOne
@JoinColumn(name = "admin_id")
private DitaAdmin admin;
@ManyToOne
@JoinColumn(name = "account_id")
private DitaAccount account;
//getters & setters
This works just fine with Hibernate 4.1.4Final :
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext(new String[] {"applicationContext.xml",
"db_beans.xml"});
DitaAdminSkillDao dispDao = applicationContext.getBean
(DitaAdminSkillDao.class);
List<DitaAdminSkill> disp = dispDao.findAll();
System.out.println(disp.get(0).getId().getAccount());
But when I try to run this with OpenJPA I get an error : Unknown column
't0.id' in 'field list'
Seems like it is ignoring the @EmbeddedId annotation and tries to select
directly the ID, which do not exists.
I have been trying to use @IdClass instead but with pretty much same
result :(
Since OpenJPA has general lack of support/forums/everything, I wasnt able
to find anything useful through google.
If anyone has any comments, please, it will be more than welcomed.
Oh, and I am using OpenJPA 2.1.5 with Maven (tried 2.2.0-SNAPSHOT but does
the same) :
<!-- Apache implementation of JPA2.0 -->
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-all</artifactId>
<version>2.1.5</version>
</dependency>
Thanks !!!!
One question, why your create statement doesnt have a primary key?
El 13/08/2012 00:39, "Zbynek Vavros" escribió:
>
> Hi,
>
> Im having trouble with composite key and OpenJPA.
> I was using hibernate so excuse my lack of OpenJPA knowledge.
>
> I have a table already created and one that I cannot modify :
>
> CREATE TABLE `admin_skills` (
> `admin_id` INT(11) NOT NULL,
> `account_id` INT(11) NOT NULL,
> `skill` INT(11) NOT NULL DEFAULT '5',
> UNIQUE INDEX `account_id` (`account_id`, `admin_id`),
> INDEX `admin_id` (`admin_id`),
> CONSTRAINT `admin_skills_ibfk_1` FOREIGN KEY (`admin_id`) REFERENCES
> `admins` (`idadmin`) ON UPDATE CASCADE ON DELETE CASCADE,
> CONSTRAINT `admin_skills_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES
> `account` (`account_id`) ON UPDATE CASCADE ON DELETE CASCADE
> )
>
> I have created following entity/key :
>
> @Entity
> @Table(name = "admin_skills")
> public class DitaAdminSkill {
>
> @EmbeddedId
> private DitaAdminSkillPK id;
>
> @Column(name = "skill")
> private Integer skill;
>
> //getters & setters
>
> @Embeddable
> public class DitaAdminSkillPK implements Serializable{
>
> @ManyToOne
> @JoinColumn(name = "admin_id")
> private DitaAdmin admin;
>
> @ManyToOne
> @JoinColumn(name = "account_id")
> private DitaAccount account;
>
> //getters & setters
>
> This works just fine with Hibernate 4.1.4Final :
>
> ApplicationContext applicationContext = new
> ClassPathXmlApplicationContext(new String[] {"applicationContext.xml",
> "db_beans.xml"});
> DitaAdminSkillDao dispDao = applicationContext.getBean
> (DitaAdminSkillDao.class);
> List<DitaAdminSkill> disp = dispDao.findAll();
> System.out.println(disp.get(0).getId().getAccount());
>
>
> But when I try to run this with OpenJPA I get an error : Unknown column
> 't0.id' in 'field list'
> Seems like it is ignoring the @EmbeddedId annotation and tries to select
> directly the ID, which do not exists.
> I have been trying to use @IdClass instead but with pretty much same
> result :(
>
> Since OpenJPA has general lack of support/forums/everything, I wasnt able
> to find anything useful through google.
>
> If anyone has any comments, please, it will be more than welcomed.
>
> Oh, and I am using OpenJPA 2.1.5 with Maven (tried 2.2.0-SNAPSHOT but does
> the same) :
>
> <!-- Apache implementation of JPA2.0 -->
> <dependency>
> <groupId>org.apache.openjpa</groupId>
> <artifactId>openjpa-all</artifactId>
> <version>2.1.5</version>
> </dependency>
>
> Thanks !!!!
>
>
>
Hello,
According to the docu I have regarding the jpa,
you need to implement equals() & hashCode() in the
embeddable. It also shows a constructor ...
public class DitaAdminSkillPK implements Serializable{
@ManyToOne
@JoinColumn(name = "admin_id")
private DitaAdmin admin;
@ManyToOne
@JoinColumn(name = "account_id")
private DitaAccount account;
public DitaAdminSkillPK(){
}
public DitaAdminSkillPK(DitaAdmin admin, DitaAccount account){
this.admin = admin;
this.account = account;
}
public boolean equals(Object o) {
return ((o instanceof DitaAdminSkillPK) &&
admin.equals(((DitaAdminSkillPK) o).getAdmin()) &&
account.equals(((DitaAdminSkillPK) o).getAccount());
}
public int hashCide() {
return admin.hashCode() + account.hashCode();
}
....
}
Don't know if that will fix it for you, but it might be worth a try !
Cheers
John> -----Ursprüngliche Nachricht-----
> Von: Zbynek Vavros
> Gesendet: Montag, 13. August 2012 07:39
> An:
> Betreff: Composite key problem
>
>
> Hi,
>
> Im having trouble with composite key and OpenJPA.
> I was using hibernate so excuse my lack of OpenJPA knowledge.
>
> I have a table already created and one that I cannot modify :
>
> CREATE TABLE `admin_skills` (
> `admin_id` INT(11) NOT NULL,
> `account_id` INT(11) NOT NULL,
> `skill` INT(11) NOT NULL DEFAULT '5',
> UNIQUE INDEX `account_id` (`account_id`, `admin_id`), INDEX
> `admin_id` (`admin_id`), CONSTRAINT `admin_skills_ibfk_1`
> FOREIGN KEY (`admin_id`) REFERENCES `admins` (`idadmin`) ON
> UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT
> `admin_skills_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES
> `account` (`account_id`) ON UPDATE CASCADE ON DELETE CASCADE
> )
>
> I have created following entity/key :
>
> @Entity
> @Table(name = "admin_skills")
> public class DitaAdminSkill {
>
> @EmbeddedId
> private DitaAdminSkillPK id;
>
> @Column(name = "skill")
> private Integer skill;
>
> //getters & setters
>
> @Embeddable
> public class DitaAdminSkillPK implements Serializable{
>
> @ManyToOne
> @JoinColumn(name = "admin_id")
> private DitaAdmin admin;
>
> @ManyToOne
> @JoinColumn(name = "account_id")
> private DitaAccount account;
>
> //getters & setters
>
> This works just fine with Hibernate 4.1.4Final :
>
> ApplicationContext applicationContext = new
> ClassPathXmlApplicationContext(new String[]
> {"applicationContext.xml", "db_beans.xml"});
> DitaAdminSkillDao dispDao = applicationContext.getBean
> (DitaAdminSkillDao.class);
> List<DitaAdminSkill> disp = dispDao.findAll();
> System.out.println(disp.get(0).getId().getAccount());
>
>
> But when I try to run this with OpenJPA I get an error :
> Unknown column 't0.id' in 'field list'
> Seems like it is ignoring the @EmbeddedId annotation and
> tries to select directly the ID, which do not exists.
> I have been trying to use @IdClass instead but with pretty
> much same result :(
>
> Since OpenJPA has general lack of support/forums/everything,
> I wasnt able to find anything useful through google.
>
> If anyone has any comments, please, it will be more than welcomed.
>
> Oh, and I am using OpenJPA 2.1.5 with Maven (tried
> 2.2.0-SNAPSHOT but does the same) :
>
> <!-- Apache implementation of JPA2.0 --> <dependency>
> <groupId>org.apache.openjpa</groupId>
> <artifactId>openjpa-all</artifactId>
> <version>2.1.5</version>
> </dependency>
>
> Thanks !!!!
>
>
>