0

I am trying to retrieve json data from SQL however ID is not being returned at all. Here is my code:

@Entity
@Table(name="Customer")
@SequenceGenerator(name="yourSequenceGenerator", initialValue=1, allocationSize=50)
public class Customer implements Serializable {
   private static final long serialVersionUID = 1L;
    @Column(name = "ID")    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long id;


public List<Customer> addAllCustomers(){

  entityManagerFactory.createEntityManager();
  TypedQuery<Customer> query = entityManager.createQuery("SELECT e FROM Customer e", Customer.class);
  List<Customer> resultList = query.getResultList();
  return resultList;
}



@POST
@Produces(MediaType.APPLICATION_JSON)
public void getProfile(@QueryParam("name") String name,
                       @QueryParam("surname") String surname
               
                       )

{
        entityManager.getTransaction().begin();
       
        c.setContactName(name);  
        c.setSurname(surname);
        entityManager.persist(c);            
        entityManager.getTransaction().commit();
        entityManager.close();
        entityManagerFactory.close();

}   

Result

[{"contactName":"bartekwsk","surname":"ss"}]

I can write and read to database, only ID is not working

sticky bit
  • 36,626
  • 12
  • 31
  • 42
Bartosz
  • 13
  • 4
  • *Unrelated:* `getProfile()`??? Doesn't seem to be "getting" anything. – Andreas Sep 25 '20 at 09:55
  • Could please post all the entire POJO/entity class? Please create getter and setter for all properties and create an empty constructor in it. And if you are going to store the customer please user save method instead of persist. – Kayis Rahman Sep 25 '20 at 09:56
  • @Andreas I am not supposed to set it, as should be generated automatically and then pulled from database. It worked that way on glassfish, now using tomcat server. – Bartosz Sep 25 '20 at 09:57
  • Please refer the below link, looks like the same issue https://stackoverflow.com/questions/9732453/jpa-returning-an-auto-generated-id-after-persist – anurag saxena Sep 25 '20 at 09:58
  • @KayisRahman I have empty constructor and all setters and getters – Bartosz Sep 25 '20 at 09:59
  • @Bartosz I'm not questioning the content of the method, I'm questioning the method **name**. `getProfile()` is a very misleading name for a **POST** handler method that **inserts/updates** the database. – Andreas Sep 25 '20 at 09:59
  • @Bartosz Use the save method instead of persist. See https://stackoverflow.com/questions/5862680/whats-the-difference-between-session-persist-and-session-save-in-hibernate – Kayis Rahman Sep 25 '20 at 10:02
  • Dear @Andreas yes, you are very correct it will be changed to postProfile(). – Bartosz Sep 25 '20 at 10:02
  • @KayisRahman persist worked very well on Glassfish. I will need to rebuild app now to use session. Do you think this is the core issue of using persist over save? – Bartosz Sep 25 '20 at 10:11
  • @Bartosz It makes a transient instance persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. You can read more from my previous comment. – Kayis Rahman Sep 25 '20 at 10:14
  • @KayisRahman unfortunately flush() or changing identity to Tale does help – Bartosz Sep 25 '20 at 10:27
  • Personally, I would prefer to use the save method if we need to insert a record to the table, and Flush is also recommended. – Kayis Rahman Sep 25 '20 at 10:32
  • @KayisRahman save method is not provided for EntityManager. I use it also to link to my persistance.xml EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("com.mycompany_xxxxxx_war_1.0-SNAPSHOTPU"); Can I map similar way with Session? Thank you in advance – Bartosz Sep 25 '20 at 10:34
  • Okay, use flush then. Out of curiosity is this hibernate or spring app? BTW, I was using the trough session in hibernate. – Kayis Rahman Sep 25 '20 at 10:55
  • @Kayis hibernate Sir, flush didn't help as well – Bartosz Sep 25 '20 at 12:39

0 Answers0