0

Say we input username: hello
The entity below save the user info in User regU

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int user_id;
    
    @Column(name = "username")
    private String username;

The database already have 3 users with (auto_increment) id 1,2 and 3

Then we run the JPA save method:

URepo.save(regU)

Would the user normally get saved with id 4?
I tried test to this in my code, but when I run the save() method, literally nothing happens, hibernate doesn't show me the query it ran, no exception thrown, no changes in database.
My other queries print to console, but not save().

My JPA has these properties:

    <property name="jpaProperties">
        <props>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="format_sql">true</prop>
            <prop key="use_sql_comments">true</prop>
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
        </props>
    </property>

Edit 1: My current code gives the new entity the id of 0, how would spring-data save an id of 0, and is it normal for the new entity to have id of 0 rather than the auto_increment value?

Nor
  • 16
  • 2
  • When you save an entry with existing `id` field, spring-data interprets this request as an update instead of create. You can look [here](https://stackoverflow.com/a/11881628/14394219) for more details. – artiomi Oct 13 '22 at 10:16
  • @artiomi I know, what I am asking is, what if I save a new entity like above? My current code gives the entity the id of 0, how would spring-data save an id of 0, and is it normal for the new entity to have id of 0 rather than the auto_increment value? – Nor Oct 14 '22 at 04:18

0 Answers0