0

The current code adds a row to the DB. If I reuse this code, it will update the existing row instead of creating a new one. How to create a new row using the same code?


SessionFactory sessionFactory = HibernateFactory.getSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();

Item item = new Item();
item.setName("name");
item.setPrice(122.3);
item.setCount(225);
session.persist(item);

session.getTransaction().commit();
session.close();
public abstract class AbstractEntity {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id")
    private Long id;
}

Item extended AbstractEntity

Chaffy
  • 182
  • 12

1 Answers1

0

Hibernate uses persistence.xml or hibernate.cfg.xml to read the database properties. To insert a new row the hibernate.hbm2ddl.auto has to be set to update instead of create-drop or create

<properties>
  <property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>

Below you will find a link with the description for every value: What are the possible values of the Hibernate hbm2ddl.auto configuration and what do they do

Chaffy
  • 182
  • 12