1

I want to open info about my contact in new window, but when i press:

<a href="/contactData/${contact.id}" target="_blank">${contact.contactName}</a>

I have such error:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165) org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286) org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) kz.adilka.springsecurity.app.model.Contact_$$_jvsta24_0.getContactName(Contact_$$_jvsta24_0.java)

here is my jsp file that must be opened:

<table class="tg">
    <tr>
        <th width="80">ID</th>
        <th width="120">Name</th>
        <th width="120">Mail</th>
        <th width="120">Phone</th>
        <th width="120">City</th>
    </tr>
        <tr>
            <td>${contact.id}</td>
            <td>${contact.contactName}</td>
            <td>${contact.contactEmail}</td>
            <td>${contact.contactPhone}</td>
            <td>${contact.contactCity}</td>
            </td>
        </tr>
</table>

method from my controller that says to open info in new window:

@RequestMapping("contactData/{id}")
public String contactData(@PathVariable("id") int id, Model model) {
    model.addAttribute("contact", this.contactService.getContactById(id));
    return "contactData";
}

my Entity:

@Entity
@Table(name = "contact")
public class Contact {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "contact_name")
    private String contactName;

    @Column(name = "contact_phone")
    private String contactPhone;

    @Column(name = "contact_city")
    private String contactCity;

    @Column(name = "contact_mail")
    private String contactEmail;

1 Answers1

0

This is because you have annotated your entity with lazy (it is obviously), but you get the data in JSP, and the connection to database is closed and no it is possible get it.

Solution 1 (recommended):

Transform your entity to DTO with data needed to show in a window.

Solution 2:

Use "Eager" to get all information, but this cannot be good if you have several information.

Alberto
  • 665
  • 1
  • 6
  • 21