0

I'm using hibernate to persist an entity. But the data is getting persisted as null, even though when the entity contains the specified field value.

@RestController
@RequestMapping("example")
public class Controller {

@Autowired
EntityManager entityManager;

@RequestMapping("/getData")
@Transactional
public void getData() {

    Members member = new Members();
    member.firstName= "SRC";
    member.lastName = "Code";
    member.major = "Computer Science";
    member.ID = 424;

    entityManager.persist(member);
}

Members class

 @Entity
    @Table(name = "members")
    public class Members {


    @Id
    public int ID;
    public String major;

    @Column(name = "firstName" , nullable = false)
    @Length(min = 0, max =20)
    public String firstName;


    @Length(min = 0, max =20)
    @Column(name = "lastName" , nullable = false)
    public String lastName;
}

But the data in the database table is getting null values for firstName and LastName fields. But the ID and major is correctly persisted.

Chandan S R
  • 61
  • 1
  • 8
  • Do not use public member variables. Make them `private` and use `getter` and `setter` – Jens Jan 02 '19 at 15:03
  • if something is persisted, then look in the log of your chosen JPA provider at the SQL invoked. aka "debugging" –  Jan 02 '19 at 15:05
  • Impossible to know what is wrong with the info provided, but some general remarks: 1) Use `Member` as class name, not `Members` 2) Use `id` for your primary key name in Java, not `ID` 3) Don't do database updates on a GET request – Wim Deblauwe Jan 02 '19 at 17:14
  • @WimDeblauwe This was a sample code just to try out things. Used Members as class name , since the name of the table in my DB is members. Not sure why this is not working for 2 fields. – Chandan S R Jan 03 '19 at 06:24
  • You might want to [enable logging of Hibernate](https://stackoverflow.com/questions/1710476/how-to-print-a-query-string-with-parameter-values-when-using-hibernate) to get more information. – Wim Deblauwe Jan 03 '19 at 06:26

0 Answers0