SpringBoot missing certain fields on auto generated repository

Hello, I have a maven springboot project using an H2 database. I have a module class with three fields; name, code and mnc You can see the first few lines of this class below
@Entity
public class Module {

@Id
@Column
private String code;

@Column
private String name;

@Column
private Boolean mnc = false;

/**
* Creates a new Module object.
*/
public Module(String code, String name, boolean mnc) {
super();
this.code = code;
this.name = name;
this.mnc = mnc;
}

public Module() {}

// Getters
public String getCode() {
return code;
}
@Entity
public class Module {

@Id
@Column
private String code;

@Column
private String name;

@Column
private Boolean mnc = false;

/**
* Creates a new Module object.
*/
public Module(String code, String name, boolean mnc) {
super();
this.code = code;
this.name = name;
this.mnc = mnc;
}

public Module() {}

// Getters
public String getCode() {
return code;
}
As you can see code has a corresponding getter (getCode() method), and with my ModuleRepository interface:
public interface ModuleRepository extends CrudRepository<Module, String> {
}
public interface ModuleRepository extends CrudRepository<Module, String> {
}
You would expect the "code" variable to be visible when making a REST request. However, no matter what I seem to do, I can only seem to get the following response with two of the three variables.
{
"_embedded" : {
"modules" : [ {
"name" : "aa",
"mnc" : false,
"_links" : {
"self" : {
"href" : "http://localhost:2800/modules/CS2800"
},
"module" : {
"href" : "http://localhost:2800/modules/CS2800"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:2800/modules"
},
"profile" : {
"href" : "http://localhost:2800/profile/modules"
}
}
}
{
"_embedded" : {
"modules" : [ {
"name" : "aa",
"mnc" : false,
"_links" : {
"self" : {
"href" : "http://localhost:2800/modules/CS2800"
},
"module" : {
"href" : "http://localhost:2800/modules/CS2800"
}
}
} ]
},
"_links" : {
"self" : {
"href" : "http://localhost:2800/modules"
},
"profile" : {
"href" : "http://localhost:2800/profile/modules"
}
}
}
As well as this, the code variable in the database is definitely updated, it just doesn't display in the json response. Thanks in advance.
12 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @jabbers! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
jabbers
jabbersOP3mo ago
Not sure if it has something to do with code being the primary key - but I need the code as a variable in the response Anyone able to help ?
the goat
the goat3mo ago
where is the request? also why do you use code for primary key? do you have a particular reason to use @Column like specific configurations for the respective column? why does mnc have a default value of false? And also for mnc why do you use the wrapper, but in the constructor you pass in a primitive?
ayylmao123xdd
ayylmao123xdd3mo ago
try switching the id annotation to some other field and see if that field appears and yea try renaming the field to something else maybe its a reserved word
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
jabbers
jabbersOP3mo ago
wdym? its a GET request on /modules code is a unique value which identifies each row of t he db, if thats what ur asking uhhh, im not too sure what u meann here. I was just making sure the correct columns were marked as they are in db because when u attempt to create a new module, the mnc is undefiined in the payload of the request from the frontend if its false. so, if mnc is null, its false, so i set default value to false i will try it thank you
JavaBot
JavaBot3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
jabbers
jabbersOP3mo ago
thank you both for ur help ill let u know how i get on literally just a typo
{
"code" : "AAA",
"mnc" : true,
"_links" : {
"self" : {
"href" : "http://localhost:2800/modules/bbb"
},
"module" : {
"href" : "http://localhost:2800/modules/bbb"
}
}
}
{
"code" : "AAA",
"mnc" : true,
"_links" : {
"self" : {
"href" : "http://localhost:2800/modules/bbb"
},
"module" : {
"href" : "http://localhost:2800/modules/bbb"
}
}
}
So, this is interesting - ur suggestion of moving the ID field to the name field actually worked So for whatever reason it just doesn't wanna show the primary key
ayylmao123xdd
ayylmao123xdd3mo ago
maybe some rest config makes it so the field with id annotation isnt included try to check that
jabbers
jabbersOP3mo ago
i dont know how i didn't find this earlier, but this explains everything. im able to expose ids for certain classes now https://stackoverflow.com/questions/77273097/json-data-doesn-t-show-the-id-column-when-running-spring-boot-project
Stack Overflow
JSON data doesn’t show the id column when running Spring Boot proje...
I’m working on a Spring Boot project where I have an entity called “Book” with an id column. I’m using Spring Data JPA and have created a repository interface called “BookRepo” that extends JpaRepo...
jabbers
jabbersOP3mo ago
thank u for ur help 😉
JavaBot
JavaBot3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?