1

I am new to Spring Boot & Hibernate & am facing some error that I request assistance in solving. I have 3 Entity created as below with Getters & Setters:

@Entity
@Table(name="Cinema_Hall")
public class CinemaHall {
    
    @Id
    @Column(name="Hall_Name")
    private String hallName;
    
    @Column(name = "Address_Line_1")
    private String addressLine1;
    
    @Column(name = "Address_Line_2")
    private String addressLine2;
    
    @Column(name = "Address_Line_3")
    private String addressLine3;
    
    @Column(name="No_Of_Screens")
    private int noOfScreens;
    
    @OneToMany(mappedBy="cinemaHall")
    private List<CinemaDetail> runningCinema = new ArrayList<> ();
}

@Entity
@Table(name = "CINEMA_DETAILS")
public class CinemaDetail {

    @Id
    @GeneratedValue
    @Column(name="Cinema_ID")
    private int cinemaID;
    @Column(name="Cinema_Name")
    private String cinemaName;
    @Column(name="Cinema_Begin_Time")
    private String beginTime;
    @Column(name="Cinema_End_Time")
    private String endTime;
    @Column(name="Cinema_Ticket_Price")
    private float ticketPrice;
    @Column(name="Cinema_Screen_No")
    private int screenNo;
    
    @OneToOne
    @JoinColumn(name="Seat_ID", referencedColumnName = "Cinema_ID")
    @Autowired
    private SeatDetail seatDetails;
    
    @ManyToOne
    @JoinColumn(name="Hall_ID", referencedColumnName="Cinema_ID")
    private CinemaHall cinemaHall;
}

@Entity
@Table(name = "Screen_Seats")
public class SeatDetail {
    
    @OneToOne(mappedBy="seatDetails")
    @Id
    @GeneratedValue
    private int ID;
    
    @Column(name = "Seat_No")
    @Convert(converter = SeatDetailConverter.class)
    private List<String> seatNo;
    
    @Column(name = "Seat_Available")
    @Convert(converter = SeatAvailabilityConverter.class)
    private List<Boolean> seatAvailability;
    
    @Transient 
    private final String ALPHABETS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
}

I have written interfaces of their respective DAOs by extending CrudRepository. The Service Class is as below:

@Service
public class CinemaHallRepository {

    private List<CinemaHall> cinemaHalls;
    @Autowired
    private CinemaHallDao cinemaHall;
    
    public CinemaHallRepository() {
        System.out.println("In CinemaHallRepository Constructor");
        addCinemaHall("Roxy", "Roxy Address 1", "Roxy Address 2", "Roxy Address 3", 3);
        addCinemaHall("Hind", "Hind Address 1", "Hind Address 2", "Hind Address 3", 4);
        addCinemaHall("iNox", "INOX Address 1", "INOX Address 2", "INOX Address 3", 5);
        
    }
}

Controller Class is as below:

@RestController
public class CinemaHallController {

    @Autowired
    private CinemaHallRepository hallRepository;
        
    @RequestMapping(method=RequestMethod.GET, value=("/cinemahalls"))
    public List<String> getCinemaHalls() {
        //hallRepository.setDefaultCinemaHalls();
        return hallRepository.getCinemaHalls();
}

When I am trying to run the Application, the following error is thrown at 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 java.lang.NullPointerException: Cannot invoke "org.hibernate.mapping.KeyValue.getColumnIterator()" because the return value of "org.hibernate.mapping.PersistentClass.getKey()" is null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1153) ~[spring-context-5.3.4.jar:5.3.4]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:907) ~[spring-context-5.3.4.jar:5.3.4]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582) ~[spring-context-5.3.4.jar:5.3.4]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.3.jar:2.4.3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.3.jar:2.4.3]
    at com.cinema.hall.ProjectCinemaHallApplication.main(ProjectCinemaHallApplication.java:10) ~[classes/:na]
Caused by: java.lang.NullPointerException: Cannot invoke "org.hibernate.mapping.KeyValue.getColumnIterator()" because the return value of "org.hibernate.mapping.PersistentClass.getKey()" is null
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:810) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:255) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:101) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processEndOfQueue(InFlightMetadataCollectorImpl.java:1823) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processFkSecondPassesInOrder(InFlightMetadataCollectorImpl.java:1767) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1655) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255) ~[hibernate-core-5.4.28.Final.jar:5.4.28.Final]
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58) ~[spring-orm-5.3.4.jar:5.3.4]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365) ~[spring-orm-5.3.4.jar:5.3.4]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409) ~[spring-orm-5.3.4.jar:5.3.4]
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396) ~[spring-orm-5.3.4.jar:5.3.4]
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341) ~[spring-orm-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.4.jar:5.3.4]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.4.jar:5.3.4]
    ... 17 common frames omitted
3
  • Which database you are using? Commented Feb 25, 2021 at 18:44
  • Spring boot version? Commented Feb 25, 2021 at 18:44
  • Hi nurgasemetey, DB is H2 & Spring Boot Version is 2.4.3 Commented Feb 26, 2021 at 8:07

1 Answer 1

2

@OneToOne(mappedBy = "" ) is used to define a shared key. The issue here is with the mapping of foreign key and primary key. Whwn you have both @Id and @OneToOne(mappedBy = "") defined for the same column in the following Entity class, Hibernate is unable to understand the mapping defined there. In all probability, you will have to changed what goes into the mappedBy= 's value part.

@Entity
@Table(name = "Screen_Seats")
public class SeatDetail {
    
    @OneToOne(mappedBy="seatDetails") // you need to remove this and define the mapping in different way
    @Id
    @GeneratedValue
    private int ID;
     ....
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.