Home > front end >  Unable to insert into table in hibernate
Unable to insert into table in hibernate

Time:03-08

I am trying to create new table and join with ManyToOne relation with my existing table below is my implementation New table

    @Entity(name="request_city_id")
    @Table(uniqueConstraints={@UniqueConstraints{columnNames={"request_id","cityId"})})
    @Data
    @NoArgsConstructor
    @FieldDefault(level=AccessLevel.PRIVATE)
    public class RequestCityId{

            @GenratedValue(strategy=SEQUENCE, generator="seq_req_city_id")
            @SequenceGenerator(name="seq_req_city_id", allocationSize=1)
            @Column(name="rc_id") 
            @Id
            long id;

            @ManyToOne
            @JoinColumn(name="request_id")
            Request request;
            String cityId;
            String status
    }

Existing table

        @Entity(name="request")
        @Data
        @NoArgsConstructor
        @FieldDefault(level=AccessLevel.PRIVATE)
        public class Request{
            String frequency
            @GenratedValue(strategy=SEQUENCE, generator="seq_req_d")
            @SequenceGenerator(name="seq_req_id", allocationSize=1)
            @Column(name="request_id") 
            @Id
            long id;

            @OneToMany(cascade={ PERSIST, MERGE}, mappedBy="request", fetch=EAGER)
            Set<RequestCityId> requestCityIds;
 
    }

but when I am trying to insert into my new table I see my hibernate query gets stuck and just gets timed out after sometime, I am not sure what I am doing wrong here? If I just kep cascade type MERGE then getting Hibernate Error: a different object with the same identifier value was already associated with the session

CodePudding user response:

First you should create an getters and setters method to each entity.

Request Class Request City Id Class

this code creates the tables and also saves the data into the table.

CodePudding user response:

Using @Data in entity classes in not recommended because it may cause some problems with jpa as mentioned here.

  • Related