Reputation:
I'm creating a Spring Boot application but my findAll (implemented with JPARepository) returns every attribute except the id, and I need the id for the view I'm trying to create. Is there any way to change this? I currently have
/model/rol.java
@Entity
@Table(name = "roles")
public class rol {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "rol", nullable = false)
private String rol;
@OneToMany(mappedBy = "rol", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Administrador> administradores;
public rol() {
}
public rol(String rol) {
this.rol = rol;
}
/* Getters and Setters */
/repository/rolrepository.java
@Repository
public interface rolrepository extends JpaRepository<rol, Long>{
}
/controller/rolcontroller.java
@Controller
public class rolcontroller {
@Autowired
private rolrepository rolRepository;
@GetMapping("/roles")
public String showAll(Model model) {
model.addAttribute("roles", rolRepository.findAll());
return "roles";
}
/templates/roles.html
<table class="table table-striped">
<thead class="thead-dark" >
<tr>
<th scope="col"> ID </th>
<th scope="col"> Rol </th>
</tr>
</thead>
<tbody>
<tr th:if="${roles.empty}">
<td colspan="2"> No hay roles registrados </td>
</tr>
<tr th:each="rol : ${roles}">
<td><span th:text="${rol.id}"> Rol </span></td>
<td><span th:text="${rol.rol}"> Rol </span></td>
</tr>
</tbody>
</table>
However, I get the error Exception evaluating SpringEL expression: "rol.id"
After some research I found out that apparently JPARepository doesn't include the id of the model in the findAll()
method.
Is there any way to modify findAll()
or any other file to be able to use the id in my HTML table?
Thanks in advance
The expected output is the rol.id value in my table, however, the actual result is Exception evaluating SpringEL expression: "rol.id"
Upvotes: 1
Views: 1762
Reputation: 41
You need to correct private int id; to Long. Also make sure that getters are available for id;
Upvotes: 0
Reputation: 1
maybe you just forgot to add getter for id in your model class along with the long type instead of int:
public long getId() {
return id;
}
Upvotes: 0
Reputation: 11
By default, springJpa does not expose ids of entities so you have to make further configurations to get into it, here is one of the solutions: create a java class, write the new config as displayed below and override the default rest repository config to expose it, the full java code will be:
// package com.aminecode.springboot.myapp.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import javax.persistence.EntityManager;
import javax.persistence.metamodel.EntityType;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
@Configuration
public class MyDataRestConfig implements RepositoryRestConfigurer {
private EntityManager entityManager;
@Autowired
public MyDataRestConfig(EntityManager theEntityManager) {
entityManager = theEntityManager;
}
private void exposeIds(RepositoryRestConfiguration config) {
// expose entity ids
// - get a list of all entity classes from the entity manager
Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities();
// - create an array of the entity type
List<Class> entityClasses = new ArrayList<>();
// - get the entity types for the entities
for (EntityType tempEntityType : entities) {
entityClasses.add(tempEntityType.getJavaType());
}
// - expose the entity ida for the array of entuity/domain types
Class[] domainTypes = entityClasses.toArray(new Class[0]);
config.exposeIdsFor(domainTypes);
}
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration
config, CorsRegistry corsRegistry) {
exposeIds(config);
}
}
Upvotes: 0
Reputation: 145
First of all rol is not proper Class name.You should start practicing naming conventions.
Second thing, when you are using JPARespository/CrudRepository,make sure that the data type of id column that you take in your POJO class and the one in repository are same. Refer this.
@Repository
public interface rolrepository extends JpaRepository<Rol, Long>{
}
Upvotes: 0
Reputation: 652
@Repository
public interface rolrepository extends JpaRepository<rol, Long>{
}
The JpaRepository arg
JpaRepository<rol, Long>
Indicates the ID is of type Long but you are using int id in your rol java code
Try using Long id in rol.java
Upvotes: 5