GioPoe
GioPoe

Reputation: 139

why is spring boot returning me a json with null for some values?

I'm making a game for practice.

I have a spring boot/Maven project connected to a MySQL database. I was able to setup an api that simply retrieves everything from my "allriddles" table. It worked before, but now the api returns a json where some values are null for some keys. The only thing i was playing with was the application.properties on the spring boot file. I was hopping between "update" and "none" for the spring.jpa.hibernate.ddl-auto. What could I have done wrong?

application.properties

spring.jpa.hibernate.format_sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 
spring.datasource.url=jdbc:mysql://localhost:3306/riddlesgame
spring.datasource.username=root
spring.datasource.password=RiddlesGame886
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.data.rest.base-path=/api/v1
server.port=8088

this is the database table and expected columns

database table and columns

this is the returned json with erronous null values

returned json with erroneous null values

here is the model

@Entity
@Table(name = "allriddles")
public class Riddle {

/*
 * ATTRIBUTES
 */
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String title;
private int difficulty;
private String prize;
private String riddlerName;
private int levels;
private String riddleDescription;

/*
 * CONSTRUCTORS
 */
public Riddle() {}
public Riddle( String title, int difficulty, String prize, String riddlerName, int levels,
        String description) {
    super();
    
    this.title = title;
    this.difficulty = difficulty;
    this.prize = prize;
    this.riddlerName = riddlerName;
    this.levels = levels;
    this.riddleDescription = description;
}
}

this is my controller that calls a service

 @RestController
 public class GetRiddles {

@Autowired
RiddlesService rs; 


@RequestMapping("/Riddles")
public List<Riddle> getAllRiddles(){
    return rs.getAllRiddles();
}

the service that calls crud

@Service
public class RiddlesService {

@Autowired
RiddlesRepository riddlesRepository;

public List<Riddle> getAllRiddles(){
    List<Riddle> riddles = new ArrayList<>();
    riddlesRepository.findAll()
    .forEach(riddles::add);
    

    return riddles;
}

the crud interface

public interface RiddlesRepository extends CrudRepository<Riddle, Integer> {

}

and finally the console output if thats of any help

trace

Upvotes: 1

Views: 3553

Answers (2)

Sunil
Sunil

Reputation: 374

This is the default way in which hibernate names columns in the table based on the fields defined in the entity.

To override the default behaviour you can use

@Column(name = "riddleName")
private String riddlerName;

@Column(name = "riddleDescription")
private String riddleDescription;

Upvotes: 1

akang
akang

Reputation: 94

I tried the same thing, and got a similar result. It occurred, as you said, upon modifying spring.jpa.hibernate.ddl-auto to update. It ended up adding 2 new columns.

Hibernate: alter table allriddles add column riddle_description varchar(255)
Hibernate: alter table allriddles add column riddler_name varchar(255)

The original column names were riddleDescription and riddlerName and those remained with existing values. But the new columns will not have data in them. So assuming I recreated the same issue you had, either you have to

  • in the database: move data from old columns (without the underscore) to new columns (with the underscore) and remove old columns. Or
  • in the database: remove new columns (with the underscore). in Riddle.java change properties riddlerName and riddleDescription to riddlername and riddledescription.

I assume possibly you might have also changed at some point between all lower case and camelcase for those property names. Because camelcase properties will map to database as underscores (e.g. code - riddleName -> db column - riddle_name), while all lowercase will not have underscores (e.g. code - riddlername -> db column - riddlerName or riddlername).

Upvotes: 1

Related Questions