user3217883
user3217883

Reputation: 1465

how to display mysql view in spring boot?

I have a Spring Boot CRUD app that is working, as long as I use regular MySQL tables. But I need to display data from multiple tables so I created a MySQL view. But getting the following error now:

Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: No identifier specified for entity: net.tekknow.medaverter.domain.AppointmentView

I was following this example: https://www.javabullets.com/calling-database-views-from-spring-data-jpa/

Here is the domain object:

package net.tekknow.medaverter.domain;

import java.io.Serializable;
import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.Size;

@Entity
@Table(name = "vw_appointments")
public class AppointmentView implements Serializable {
    @Size(max = 32)
    @Column(name="Date")
    public String date;

    @Column(name="Physician")
    public String physician;

    @Column(name="LabCollected")
    public Timestamp labCollected;

    @Column(name="Note")
    public String note;

    public String getDate_time() {
        return date;
    }
    public String getPhysician() {
        return physician;
    }
    public Timestamp getDatetime_collected() {
        return labCollected;
    }
    public String getNote() {
        return note;
    }
}

Here is a mysql query of the view, just to show you it works:

mysql> select * from vw_appointments; +------------+-------------+---------------------+-------------------+ | Date | Physician | LabCollected | Note | +------------+-------------+---------------------+-------------------+ | 10/29/2010 | CAMPBELL, J | 2010-10-29 11:09:00 | no note available | +------------+-------------+---------------------+-------------------+ 1 row in set (0.02 sec)

Here is the service code:

@Service
@Transactional
public class AppointmentViewService {

    @Autowired
    AppointmentViewRepository repo;

    public List<AppointmentView> listAll() {
        return repo.findAll();
    }      
}

Here is the repository code:

public interface AppointmentViewRepository extends JpaRepository<AppointmentView,Integer> {}

Does Hibernate not handle views? Suggestions?

Upvotes: 2

Views: 6619

Answers (1)

Olivier Depriester
Olivier Depriester

Reputation: 1625

Everything is said in the error message :

No identifier specified for entity: net.tekknow.medaverter.domain.AppointmentView

Design your view so it has 1 column that can be mapped as an identifier field of the entity by using the @Id annotation.

You can also add the @org.hibernate.annotations.Immutable annotation to your entity to ensure that Hibernate won't try to propagate the changes you could make to your entity

@Entity
@Table(name = "vw_appointments")
// Prevent changes from being applied by Hibernate
@org.hibernate.annotations.Immutable
public class AppointmentView implements Serializable {
    // Identifier. Has to be Integer as you implement JpaRepository<AppointmentView,Integer>
    @Id
    @Column(name="Appointment_Id")
    private Integer appointmentId;

    public Integer getAppointmentId() {
        return this.appointmentId;
    }

    public void setAppointmentId(Integer appointmentId) {
        this.appointmentId = appointmentId;
    }

    // Public attributes ???. 
    // If it is not mandatory for technical reasons, prefer private attributes + getter/setter
    @Size(max = 32)
    @Column(name="Date")
    public String date;

    @Column(name="Physician")
    public String physician;
    ...
}

Upvotes: 5

Related Questions