Try turning on SQL trace to see what SQL is being generated from your JPQL.

Rick



On Sep 8, 2009, at 3:54 AM, "B.L. Zeebub" <[email protected]> wrote:




Jean-Baptiste BRIAUD -- Novlog wrote:

Did you try GenerationType.IDENTITY ?
@GeneratedValue(strategy = GenerationType.IDENTITY)

I think it should work with int but I prefer long instead.


GenerationType.IDENTITY appears to make no difference. However, suprisingly
(to me anyway)

@Test
   public void getPlatformById() {
       Platform platform = (Platform) em.find(Platform.class, 8);
       assertNotNull(platform);
}

works and returns the correct platform entity although platform.getId ()
still returns 0.

@Test
   public void getPlatformByQueryById() {
       String jpql = "Select c from Platform c where c.id = '8'";
Platform platform = (Platform) em.createQuery (jpql).getSingleResult();
       assertNotNull(platform);
}

This fails with a no instance found exception from OpenJPA

Regards

On Sep 8, 2009, at 10:56 , B.L. Zeebub wrote:


Hi

I have attached my entity definition below. This was created over a
pre-existing database. I create the EntityManagerFactory to obtain the
entitymanager (em) as normal and execute the code;

String jpql = "select c from Platform c";
       em.getTransaction().begin();
       entities = (List) em.createQuery(jpql).getResultList();
       em.getTransaction().commit();

I get the list of Platform entities as expected. Looping through the
list,
getPlatformName() returns the text as expected, but getId() always
returns 0
(zero) for all members of the list. Any ideas as to what I've missed.

Regards

@Entity
@Table(name="Platform")
public class Platform implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   @Column(name="id", unique=true, nullable=false)
   private int id;

   @Column(name="PlatformName", length=32)
   private String platformName;

   //bi-directional many-to-one association to PEcu
   @OneToMany(mappedBy="platform")
   private Set<PEcu> pecus;

  public Platform() {
  }

   public int getId() {
       //TODO: Remove
       System.out.println("Platform.getId() called - " + this.id);
       return this.id;
   }

   public void setId(int id) {
       this.id = id;
   }

   public String getPlatformName() {
       //TODO: Remove
       System.out.println("Platform.getPlatform() called - " +
this.platformName);
       return this.platformName;
   }

   public void setPlatformName(String platformName) {
       this.platformName = platformName;
   }

   public Set<PEcu> getPecus() {
       return this.pecus;
   }

   public void setPecus(Set<PEcu> pecus) {
       this.pecus = pecus;
   }

}
--
View this message in context:
http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3601995.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.





--
View this message in context: 
http://n2.nabble.com/NEWBIE-OpenJPA-Cannot-get-id-field-returned-tp3601995p3602236.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Reply via email to