简体   繁体   中英

Spring Boot Batch DB to CSV Invalid property 'ID' of bean class Bean property 'ID' is not readable or has an invalid getter method

I tried to implement an example of Spring Batch processing. When I send a request, Normally all values from database is loaded to the csv file but I got this error shown below.

org.springframework.beans.NotReadablePropertyException: Invalid property 'ID' of bean class [com.example.springbatchprocessdbtocsv.model.User]: Bean property 'ID' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

As There is a problem in data.sql, I inserted all values manually.

Here is the User class

@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "USERS")
@Builder
public class User extends BaseEntity{

    @Type(type = "org.hibernate.type.UUIDCharType")
    private UUID personId;

    private String firstName;
    private String lastName;
    private String email;

    @Enumerated(EnumType.STRING)
    private Gender gender;

    private String country;

    @JsonFormat(pattern="yyyy-MM-dd")
    private LocalDate birthday;

    private int age;
}

Here is the enum of Gender?

public enum Gender {
    MALE,
    FEMALE
}

Here is the BaseEntity class?

@MappedSuperclass
@Getter
public class BaseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
}

Here is the script of creating database.

CREATE TABLE USERS (
    "ID" BIGINT auto_increment NOT NULL PRIMARY KEY,
    "AGE" INTEGER NOT NULL,
    "BIRTHDAY" DATE,
    "COUNTRY" CHARACTER VARYING(255),
    "EMAIL" CHARACTER VARYING(255),
    "FIRST_NAME" CHARACTER VARYING(255),
    "GENDER" CHARACTER VARYING(255),
    "LAST_NAME" CHARACTER VARYING(255),
    "PERSON_ID" CHARACTER VARYING(255)
);

Here is the UserProcessor shown below

public class UserProcessor implements ItemProcessor<User, User> {

    @Override
    public User process(User user) throws Exception {
        //return user;

        // I also tried it but nothing changed.
        User user = User.builder()
            .personId(from.getPersonId())
            .firstName(from.getFirstName())
            .lastName(from.getLastName())
            .email(from.getEmail())
            .country(from.getCountry())
            .birthday(from.getBirthday())
            .gender(from.getGender().equals("Male") ? Gender.MALE : Gender.FEMALE)
            .age(from.getAge())
            .build();

    return user;
    }
}

How can I fix the issue?

Here is the example link: Link

Here is the answer shown below.

After I changed column names as lowercase, the issue disappeared.

@Bean
    public FlatFileItemWriter<User> writer() {

        FlatFileItemWriter<User> writer = new FlatFileItemWriter<>();

        writer.setResource(outputResource);
        writer.setAppendAllowed(true);

        writer.setLineAggregator(new DelimitedLineAggregator<User>() {
            {
                setDelimiter(",");
                setFieldExtractor(new BeanWrapperFieldExtractor<User>() {
                    {
                        setNames(new String[]{"id", "age", "birthday", "country", "email", "firstName", "gender", "lastName", "personId"});
                    }
                });
            }
        });

        return writer;
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM