Hibernate mapping error

Asked

Viewed 43 times

-3

Good afternoon person, could help me in a mapping error?

Event class:

@Entity
@Table(name = "EVENT")
public class Event {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    private long idEvent;

    @Column(name = "name", nullable = false, length = 80)
    @NotBlank
    private String name;

    @Column(name = "DATE_HOUR", nullable = false)
    @NotBlank
    private Date dateHour;

    @Column(name = "AMOUNT_TICKET", nullable = false)
    @NotBlank
    private int amountTicket;

    @Column(name = "VALUE_TICKET", nullable = false)
    @NotBlank
    private double valueTicket;

    @Column(name = "EVENT_EXPIRE", nullable = false)
    @NotBlank
    private Date eventExpire;

    @Column(name = "description", nullable = false, length = 300)
    @NotBlank
    @Lob
    private String description;

    @ManyToOne
    @JoinColumn(name = "EVENT_PLACE", nullable = false)
    private EventPlace place;

    @OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
    private List<Ticket> ticket = new ArrayList<>();

    public long getIdEvent() {
        return idEvent;
    }

    public void setIdEvent(long idEvent) {
        this.idEvent = idEvent;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getDateHour() {
        return dateHour;
    }

    public void setDateHour(Date dateHour) {
        this.dateHour = dateHour;
    }

    public int getAmountTicket() {
        return amountTicket;
    }

    public void setAmountTicket(int amountTicket) {
        this.amountTicket = amountTicket;
    }

    public double getValueTicket() {
        return valueTicket;
    }

    public void setValueTicket(double valueTicket) {
        this.valueTicket = valueTicket;
    }

    public Date getEventExpire() {
        return eventExpire;
    }

    public void setEventExpire(Date eventExpire) {
        this.eventExpire = eventExpire;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public EventPlace getPlace() {
        return place;
    }

    public void setPlace(EventPlace place) {
        this.place = place;
    }

    public List<Ticket> getTicket() {
        return ticket;
    }

    public void setTicket(List<Ticket> ticket) {
        this.ticket = ticket;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

Placeevent class:

@Entity
@Table(name = "EVENT_PLACE")
public class EventPlace {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private long idPlace;

    @Column(name = "NAME", nullable = false, length = 150)
    @NotBlank
    private String name;

    @Column(name = "ADDRESS", nullable = false, length = 150)
    @NotBlank
    private String address;

    @Column(name = "CAPACITY")
    private int capacity;

    @Column(name = "cep", nullable = false, length = 35)
    private String cep;

    @OneToMany(mappedBy = "eventplace", cascade = CascadeType.ALL)
    private List<Event> event = new ArrayList<>();

    public long getIdPlace() {
        return idPlace;
    }

    public void setIdPlace(long idPlace) {
        this.idPlace = idPlace;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getCapacity() {
        return capacity;
    }

    public void setCapacity(int capacity) {
        this.capacity = capacity;
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        this.cep = cep;
    }

    public List<Event> getEvent() {
        return event;
    }

    public void setEvent(List<Event> event) {
        this.event = event;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

}

The error generated in the console:

org.springframework.beans.factory.BeanCreationException: 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: mappedBy reference an unknown target entity property: com.qintess.apirest.queEvento.models.Event.eventplace in com.qintess.apirest.queEvento.models.EventPlace.event
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1108) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.6.RELEASE.jar:2.2.6.RELEASE]
    at com.qintess.apirest.queEvento.ApirestQueEventoApplication.main(ApirestQueEventoApplication.java:10) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_241]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_241]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_241]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_241]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.2.6.RELEASE.jar:2.2.6.RELEASE]
Caused by: org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.qintess.apirest.queEvento.models.Event.eventplace in com.qintess.apirest.queEvento.models.EventPlace.event
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:844) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:795) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:53) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1693) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1661) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:286) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1202) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1233) ~[hibernate-core-5.4.12.Final.jar:5.4.12.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:391) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:378) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.5.RELEASE.jar:5.2.5.RELEASE]
    ... 21 common frames omitted

1 answer

0

The code problem was in relationship mapping. Also, I switched @Notblank to @Notnull in the Date and Integer attributes. The @Notblank not suitable for these types.

I put the example on Github with a unit test to persist in a bank.

public class EventPlace {
        @OneToMany(mappedBy = "place")
        private List<Event> event = new ArrayList<>();
    }



    public class Event {
        @ManyToOne
        @JoinColumn(name = "EVENT_PLACE_IDPLACE", referencedColumnName = "id")
        private EventPlace place;
    }

//complete classes

@Entity 
@Table(name = "EVENT_PLACE") 
public class EventPlace {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "ID", unique = true, nullable = false)
    private long idPlace;

    @Column(name = "NAME", nullable = false, length = 150)
    @NotBlank
    private String name;

    @Column(name = "ADDRESS", nullable = false, length = 150)
    @NotBlank
    private String address;

    @Column(name = "CAPACITY")
    private int capacity;

    @Column(name = "cep", nullable = false, length = 35)
    private String cep;

    @OneToMany(mappedBy = "place")
    private List<Event> event = new ArrayList<>();

    //getters and setters

}


    @Entity
    @Table(name = "EVENT") 
    public class Event {

        private static final long serialVersionUID = 1L;

        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "ID", unique = true, nullable = false)
        private long idEvent;

        @Column(name = "name", nullable = false, length = 80)
        @NotBlank
        private String name;

        @Column(name = "DATE_HOUR", nullable = false)
        @NotNull
        private Date dateHour;

        @Column(name = "AMOUNT_TICKET", nullable = false)
        @NotNull
        private int amountTicket;

        @Column(name = "VALUE_TICKET", nullable = false)
        @NotNull
        private Double valueTicket;

        @Column(name = "EVENT_EXPIRE", nullable = false)
        @NotNull
        private Date eventExpire;

        @Column(name = "description", nullable = false, length = 300)
        @NotBlank
        @Lob
        private String description;

        @ManyToOne
        @JoinColumn(name = "EVENT_PLACE_IDPLACE", referencedColumnName = "id")
        private EventPlace place;


    //getters and setters
}

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.