5

I'm using SpringBoot, Spring data and postgresql.

In postgres I created one table.

CREATE EXTENSION "uuid-ossp";
CREATE TABLE user (
    id serial PRIMARY KEY not null,          
    user_id varchar(255) not null default uuid_generate_v4()
);   

Than create entity

@Entity
public class User implements Serializable {

  private static final long serialVersionUID = 9129894781337596497L;

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Column(columnDefinition = "serial")
  private Integer id;

  @Column(insertable = false)
  private String userId;

  public static User create(){
    User user = new User();
    return user;
  }

  public String getUserId(){
    return userId;
  }

}

Than create jpa repository like this

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {}

When I save the user I need to get id and userId but I get null instead and in database is generated UUID.

User user = userRepository.save(User.create());
assertThat(user.getUserId()).isNotNull();

How can I get those generated values from database?

7
  • The primary key is id not user_id, you should check assertThat(user.getId()).isNotNull(). Commented Jul 6, 2017 at 12:47
  • I want to get userId. And even if I save and after that try with .findOne(user.getId()) the userId, generated UUID form database, is not retrieved. Commented Jul 6, 2017 at 13:30
  • What happens if you change the table like this: user_id varchar(255) not null default 'abc' ? In that case will you get 'abc' or null? Commented Jul 6, 2017 at 13:37
  • with 'abc' is still null... Commented Jul 6, 2017 at 14:16
  • on this page: docs.jboss.org/hibernate/orm/5.0/topical/html/generated/… It woks with this Hibernate anottattion @Generated(GenerationTime.INSERT) Commented Jul 6, 2017 at 14:56

2 Answers 2

6

You have to re-read the inserted entity OR (since Hibernate 3.2?) you can specify @Generated(GenerationTime.INSERT) on the column ( https://docs.jboss.org/hibernate/orm/5.0/topical/html/generated/GeneratedValues.html#_legacy_support_in_database_generation ), which causes hibernate to re-read the attribute value after insert.

I've created a little demo:

http://peter.risko.hu/java_incubator/spring_data_postgres_uuid.zip

The User entity:

@Id
@SequenceGenerator(name="seq", sequenceName="seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq")
@Column(columnDefinition = "serial")
private Integer id;

@Column(insertable = false, name="user_id")
private String userId;

public String getUserId(){
    return userId;
}

public static User create(){
    User user = new User();
    log.info("user created in memory: " + user);
    return user;
}

public Integer getId(){
    return id;
}

@Override
public String toString() {
    return "[id: " + id + ", userId: " + userId + "]";
}

The test:

@Test
public void testInsert() {
    User u = userRepository.save(User.create());
    log.info("user after save: " + u);
    log.info("users queried from db: " + userRepository.findAll());
}

The output:

tst.User - user created in memory: [id: null, userId: null]
tst.UserRepositoryTest - user after save: [id: 960, userId: null]
tst.UserRepositoryTest - users queried from db: [[id: 960, userId: dc7cdc53-984d-4051-a3f3-ecc7136d9fd8]]

If I apply the @Generated(GenerationTime.INSERT):

@Column(insertable = false, name="user_id")
@Generated(GenerationTime.INSERT)
private String userId;

Then the result is:

tst.User - user created in memory: [id: null, userId: null]
tst.UserRepositoryTest - user after save: [id: 961, userId: c68ea300-2f20-47ea-a76d-6e1aaf97a5ea]
tst.UserRepositoryTest - users queried from db: [[id: 960, userId: dc7cdc53-984d-4051-a3f3-ecc7136d9fd8], [id: 961, userId: c68ea300-2f20-47ea-a76d-6e1aaf97a5ea]]
Sign up to request clarification or add additional context in comments.

Comments

0

You should add missing getters and setters for properties id and userId into the User class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.