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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.