ArchiveOrangemail archive

users.openjpa.apache.org


(List home) (Recent threads) (2 other Apache OpenJPA lists)

Subscription Options

  • RSS or Atom: Read-only subscription using a browser or aggregator. This is the recommended way if you don't need to send messages to the list. You can learn more about feed syndication and clients here.
  • Conventional: All messages are delivered to your mail address, and you can reply. To subscribe, send an email to the list's subscribe address with "subscribe" in the subject line, or visit the list's homepage here.
  • Low traffic list: less than 3 messages per day
  • This list contains about 10,762 messages, beginning Jun 2007
  • 0 messages added yesterday
Report the Spam
This button sends a spam report to the moderator. Please use it sparingly. For other removal requests, read this.
Are you sure? yes no

Composite key problem

Ad
Zbynek Vavros 1344836371Mon, 13 Aug 2012 05:39:31 +0000 (UTC)
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 !!!!
José Luis Cetina 1344859377Mon, 13 Aug 2012 12:02:57 +0000 (UTC)
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 !!!!
>
>
>
Boblitz John 1344862450Mon, 13 Aug 2012 12:54:10 +0000 (UTC)
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 !!!!
> 
> 
>
Home | About | Privacy