ObjectEntity.java

import org.hibernate.annotations.*; import org.hibernate.annotations.NamedQueries; import org.hibernate.annotations.NamedQuery; import javax.persistence.*; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.NotNull; import java.io.Serializable; import java.util.ArrayList; import java.util.List; @Entity @Table(name = "nc_object") @NamedQueries({ @NamedQuery(name = "getObjectsByObjectType", query = "from ObjectEntity oe where oe.objectType = :objectType"), @NamedQuery(name = "getObjectsByParent", query = "from ObjectEntity oe where oe.parent = :parent") }) public class ObjectEntity implements Serializable { private static final long serialVersionUID = 3720088500236365064L; @Id @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid2") @Column(name = "object_id") private String id; @NotNull @Column(name = "object_name") private String name; @NotNull @ManyToOne @JoinColumn(name = "object_type") private ObjectType objectType; @ManyToOne @JoinColumn(name = "parent_id") private ObjectEntity parent; @Transient private List<ParamEntity> paramEntityList = new ArrayList<>(); @Transient private List<AttributeEntity> attributeEntityList = new ArrayList<>(); public ObjectEntity() { } public ObjectEntity(String id, String name, ObjectType objectType, ObjectEntity parent) { this.id = id; this.name = name; this.objectType = objectType; this.parent = parent; } public ObjectEntity(String name, ObjectType objectType, ObjectEntity parent) { this.name = name; this.objectType = objectType; this.parent = parent; } public ObjectEntity(String name, ObjectType objectType) { this.name = name; this.objectType = objectType; } public static long getSerialVersionUID() { return serialVersionUID; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public ObjectType getObjectType() { return objectType; } public void setObjectType(ObjectType objectType) { this.objectType = objectType; } public ObjectEntity getParent() { return parent; } public void setParent(ObjectEntity parent) { this.parent = parent; } public List<ParamEntity> getParamEntityList() { return paramEntityList; } public void setParamEntityList(List<ParamEntity> paramEntityList) { this.paramEntityList = paramEntityList; } public List<AttributeEntity> getAttributeEntityList() { return attributeEntityList; } public void setAttributeEntityList(List<AttributeEntity> attributeEntityList) { this.attributeEntityList = attributeEntityList; } @Override public String toString() { return "object {" + "name=" + name + ", " + "type=" + objectType.toString() + ", " + "parent=" + (parent != null ? parent.toString() : "not parent") + "}"; } } 

It contains the paramEntityList collection:

ParamEntity.java

 @Entity @Table(name = "nc_params") @IdClass(ParamPk.class) @org.hibernate.annotations.NamedQueries({ @NamedQuery(name = "getParamForObject", query = "from ParamEntity pe where pe.objectId = :id") }) public class ParamEntity implements Serializable{ private static final long serialVersionUID = -1749537130852076187L; @Id @ManyToOne @JoinColumn(name= "object_id") private ObjectEntity objectId; @Id @ManyToOne @JoinColumn(name= "attribute_id") private AttributeEntity attributeId; @Column(name = "string_value") private String stringValue; @Column(name = "number_value") private Integer numberValue; @Column(name = "date_value") private Date dateValue; @JoinColumn(name= "reference_value") @ManyToOne private ObjectEntity objectEntityValue; public ParamEntity() { } public ParamEntity(ObjectEntity objectId, AttributeEntity attributeId, String stringValue, Integer numberValue, Date dateValue, ObjectEntity objectEntityValue) { this.objectId = objectId; this.attributeId = attributeId; this.stringValue = stringValue; this.numberValue = numberValue; this.dateValue = dateValue; this.objectEntityValue = objectEntityValue; } public static long getSerialVersionUID() { return serialVersionUID; } public ObjectEntity getObjectId() { return objectId; } public void setObjectId(ObjectEntity objectId) { this.objectId = objectId; } public AttributeEntity getAttributeId() { return attributeId; } public void setAttributeId(AttributeEntity attributeId) { this.attributeId = attributeId; } public String getStringValue() { return stringValue; } public void setStringValue(String stringValue) { this.stringValue = stringValue; } public Integer getNumberValue() { return numberValue; } public void setNumberValue(Integer numberValue) { this.numberValue = numberValue; } public Date getDateValue() { return dateValue; } public void setDateValue(Date dateValue) { this.dateValue = dateValue; } public ObjectEntity getObjectEntityValue() { return objectEntityValue; } public void setObjectEntityValue(ObjectEntity objectEntityValue) { this.objectEntityValue = objectEntityValue; } 

In the controller, I get a collection of such objects and pass it to the view:

 @Controller @RequestMapping("/Club") public class ClubController { public static final Logger log = Logger.getLogger(ClubController.class); @Autowired private ObjectService objectService; @RequestMapping(method = RequestMethod.GET) public String getClubs(Model model) { log.info("view Clubs..."); List<ObjectEntity> objectEntities = objectService.getObjectByTypeName("Club"); model.addAttribute("clubList", objectEntities); return "club/view"; } } 

I try to display in a loop, but the values ​​are not displayed, although empty fields are created by the number of parameters. That is, the ${param.stringValue} parameter is empty:

 <%@include file="../header.jsp"%> <h2>Clubs</h2> <table> <thead> <tr> <th>Name</th> <th>City</th> </tr> </thead> <tbody> <c:forEach var="club" items="${clubList}"> <tr> <c:forEach var="param" items="${club.paramEntityList}"> <th><a href="/lab5/Club/${club.id}">${param.stringValue}</a></th> </c:forEach> </tr> </c:forEach> </tbody> </table> <%@include file="../footer.jsp"%> 

Through the logs, I displayed the values ​​of all the attributes, so I’ll get everything from the database, but how to display it on the JSP page.

    1 answer 1

    The problem is that the loop variable has the same name as the implicit param object storing the request parameters. But it cannot be blocked, therefore, in the body of the cycle, the reference is not to the loop variable, but to the global object. Since the latter does not have a getStringValue method, you get an empty space. Just use another name for the loop variable:

     <c:forEach var="paramEntity" items="${club.paramEntityList}"> <th><a href="/lab5/Club/${club.id}">${paramEntity.stringValue}</a></th> </c:forEach> 

    PS It is a little strange that th used in your tbody and that the number of columns in each row is different.

    • I did not think about global variables, this is my ignorance. That's right, instead of th> td. And the number of columns is the same, for an object that I pull from the base, two parameters are the name and the city. And thank you for your help) - Evgeny Tupikov