1

Consider that i have a entity.

Class Employee {
@Id 
private integer id; 
private String name; 
private Timestamp effectiveFrom;
}

and i have a list of value to it..

List<Employee> Employees = new ArrayList<>();

[1,"Employee1", "2019-10-10 00:00:00.000"]
[1,"Employee2", null]
[1,"Employee3", "2019-10-10 00:00:00.000"]
[1,"Employee4", "2019-10-10 00:00:00.000"]

When i do - repository.saveAll(Employees);

The first and second employee are saved correctly from third employee on wards the effectiveFrom column (Timestamp) - is getting null..

Is it expected behavior by Spring Data JPA ?

2
  • How did you map the string to a java.sql.Timestamp? Commented Sep 8, 2020 at 18:55
  • Timestamp.ValueOf(StringValue) Commented Sep 8, 2020 at 19:13

1 Answer 1

1

First you should not use the same id for any object. I would suggest to add the @GeneratedValue annotation for the primary key. I have create the entity with Lombok like that:

@Entity
@Data
@NoArgsConstructor
class Employee {

    @Id
    @GeneratedValue
    private Integer id;
    private String name;
    private Timestamp effectiveFrom;

    public Employee(String name, String effectiveFrom) {
        this.name = name;
        this.effectiveFrom = effectiveFrom == null ? null : Timestamp.valueOf(effectiveFrom);
    }
}

At the end I create a simple jpa repository and save all entities:

@Bean
CommandLineRunner run(EmployeeRepository employeeRepository) {
    return args -> {
        List<Employee> employees = List.of(new Employee("Employee1", "2019-10-10 00:00:00.000"),
                new Employee("Employee2", null), new Employee("Employee3", "2019-10-10 00:00:00.000"),
                new Employee("Employee4", "2019-10-10 00:00:00.000"));

        employeeRepository.saveAll(employees);
        employeeRepository.findAll().forEach(System.out::println);
    };
}

The console output looks like that:

Employee(id=1, name=Employee1, effectiveFrom=2019-10-10 00:00:00.0)
Employee(id=2, name=Employee2, effectiveFrom=null)
Employee(id=3, name=Employee3, effectiveFrom=2019-10-10 00:00:00.0)
Employee(id=4, name=Employee4, effectiveFrom=2019-10-10 00:00:00.0)

Please have a look at this short example. If you cannot find the error in your code, please post your code so I can have a look.

Sign up to request clarification or add additional context in comments.

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.