2

Here I have tried to get auto Generated ID after data is persist in mysql db.

DTO Class

public class TradeTransaction {

    private long tradetransactionid ;
    private long borrowerid ;
    private String operationalcountry ;
    private String productcategory ;
    private String producttype ;
    private boolean ishazardous;
    private boolean specialisedHandle;
    private String currency;
    private double transactionvalue;
    private String suppliercountry;

    @Id
    @Column
    public long getTradetransactionid() {
        return tradetransactionid;
    }
    @Column
    public long getBorrowerid() {
        return borrowerid;
    }

Business Logic Class To persist into the Database.

        EntityManager v_objEm;
        EntityTransaction v_objTran;
        TradeTransaction v_ObjectToPersist;
        v_ObjectToPersist = new TradeTransaction();

        v_ObjectToPersist.setBorrowerid(25);
        v_ObjectToPersist.setOperationalcountry("India");
        v_ObjectToPersist.setProductcategory("electornics");
        v_ObjectToPersist.setProducttype("E9088"));
        v_ObjectToPersist.setIshazardous(true);
        v_ObjectToPersist.setSpecialisedHandle(true);
        v_ObjectToPersist.setCurrency("usd");
        v_ObjectToPersist.setTransactionvalue(2000.3);
        v_ObjectToPersist.setSuppliercountry("african-based");
        v_objEm = MyCustomClass.getEntityManager();
        v_objTran = v_objEm.getTransaction();
        v_objTran.begin();

        v_objEm.persist(v_ObjectToPersist);
        System.out.println(v_ObjectToPersist.getTradetransactionid()); //Transaction ID print as Zero.
        v_objTran.commit(); 
        System.out.println(v_ObjectToPersist.getTradetransactionid()); //Transaction ID print as Zero Here Too.

        //v_objEm.refresh(v_ObjectToPersist);  //not Working throwing Exception.
        v_objEm.close();

I have tried different flavor of code to get auto generated Id from the Object which is just persisted into the database. but didn't succeed.

Note :- This code is working fine and persisting the data into the db (No Error). But Won't be able to get auto generated ID (which is a primary key have multiple reference in tables). Links that I have tried So Far

  1. jpa-returning-an-auto-generated-id-after-persist

  2. how-to-get-id-of-last-persisted-entity-using-jpa

  3. how-to-get-the-primary-key-of-any-jpa-entity

Please help me out.. Any Help will be appriciated..

Thank You.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52

4 Answers4

4

This is a long shot, but I'll suggest it anyway.

You have declared the field private long tradetransactionid; which is a primitive type. This means it does not allow nulls and is implicitly 0 on instantiation. When you persist the object in the database, you try to insert an item/object with id equal to 0. You could try private Long tradetransactionid;, which is a reference type. This way it is initially null and can be set by auto-generation.

  • Its working fine to me . data will able persist into the database. please read the question carefully. I have mentioned as a note Thank You – Vikrant Kashyap Jun 13 '16 at 07:49
  • I understand the data is able to persist. But maybe the id in the database is auto-generated and not overridden with 0. This does not mean that JPA will override the id value in the java object if it is already set. – Vlad Călin Buzea Jun 13 '16 at 07:54
1

To use MySQL AUTO_INCREMENT you need to mark your PK field as

@GeneratedValue(strategy=GenerationType.IDENTITY)

and you haven't. Without that JPA will not know that the id has been assigned in the datastore

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
0
entityManager.persist(model);
model.getTradetransactionid();

and try..

 entityManager.flush();
Jason Christ
  • 125
  • 7
  • would you please edit your answer and write complete code. because I have already tried the way you have suggested but won't be able to succeed to get ID. Thank You @JasonChrist – Vikrant Kashyap Jun 13 '16 at 07:59
0

To use MySQL auto generated id you need to mark your primary key as

@GeneratedValue(strategy=GenerationType.AUTO)
Deepanjan
  • 649
  • 2
  • 8
  • 15