JoiningTables using jpa and hibernate problem is mulitmapping failed

Tasy Androes :

I just started learning java ee two months ago and I am struggling in some parts as showing below. I have three entity classes for a booking system and struggling in running the project after I had done some logic:

     @Entity
     @Table(name="booking")
     public class Booking implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     private int reservationId;
     private String stateroomType;
     private double totalAmount;
     private int totalGuests;   
     private int shipId;
     private int passId;

  //Joining Tables
  @OneToOne
  @JoinColumn(name="passId")
  private Passenger passenger;

  @ManyToOne
  @JoinColumn(name="shipId")
  private Cruise cruise;

    @Entity
     @Table(name = "shipcruise")
    public class Cruise implements Serializable {


    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int cruiseId;
    private String cruiseName;
    private LocalDate startDate;
    private LocalDate endDate;
    private Timestamp destination;

    @Entity
    @Table(name = "passengers")
    public class Passenger implements Serializable{

    @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int passengerId;
    private String userName;
    private String firstname;
    private String lastname;
    private String address;
    private String city;
    private String country;
    private String postalCode;
    private String password;

When I run my project I get this error message:

Exception [EclipseLink-48] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException Exception Description: Multiple writable mappings exist for the field [booking.SHIPID]. Only one may be defined as writable, all others must be specified read-only. Mapping: org.eclipse.persistence.mappings.OneToOneMapping[cruise] Descriptor: RelationalDescriptor(com.springmvc.jpa.booking.Booking --> [DatabaseTable(booking)]) Exception [EclipseLink-48] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DescriptorException Exception Description: Multiple writable mappings exist for the field [booking.PASSID]. Only one may be defined as writable, all others must be specified read-only. Mapping: org.eclipse.persistence.mappings.OneToOneMapping[passenger] Descriptor: RelationalDescriptor(com.springmvc.jpa.booking.Booking --> [DatabaseTable(booking)])

.................................................................................................

I understand that there is a problem in the mapping and I have made some research about it but still can not get the idea of how to solve it or how to make the relationship between the entity classes. Can anyone help me figure out the problem and solve it.

Database tables:

      CREATE TABLE `booking` (
     `reservationId` int NOT NULL,
     `stateroomType` varchar(30) NOT NULL,
     `totalGuests` int NOT NULL,
     `totalAmount` decimal(10,2) NOT NULL,
     `passId` int DEFAULT NULL,
     `shipId` int DEFAULT NULL,
      PRIMARY KEY (`reservationId`),
      KEY `passId` (`passId`),
      KEY `shipId` (`shipId`),
      CONSTRAINT `booking_ibfk_1` FOREIGN KEY (`passId`) REFERENCES 
      `passengers` (`passengerId`),
      CONSTRAINT `booking_ibfk_2` FOREIGN KEY (`shipId`) REFERENCES 
      `shipcruise` (`cruiseId`)
       ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

      CREATE TABLE `passengers` (
     `passengerId` int NOT NULL AUTO_INCREMENT,
     `userName` varchar(50) DEFAULT NULL,
     `password` varchar(25) DEFAULT NULL,
     `firstname` varchar(30) DEFAULT NULL,
     `lastname` varchar(30) DEFAULT NULL,
     `address` varchar(255) DEFAULT NULL,
    `city` varchar(25) DEFAULT NULL,
    `postalCode` varchar(10) DEFAULT NULL,
     `country` varchar(20) DEFAULT NULL,
     PRIMARY KEY (`passengerId`)
     ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 
   COLLATE=utf8mb4_0900_ai_ci;

    CREATE TABLE `shipcruise` (
   `cruiseId` int NOT NULL AUTO_INCREMENT,
   `CruiseName` varchar(50) DEFAULT NULL,
   `shipName` varchar(50) DEFAULT NULL,
  `startDate` date NOT NULL,
  `endDate` date NOT NULL,
   `destination` timestamp NOT NULL,
   PRIMARY KEY (`cruiseId`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Matheus Rambo :

Booking entity.

 @Entity
 @Table(name="booking")
 public class Booking implements Serializable {
 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Integer reservationId;
 private String stateroomType;
 private double totalAmount;
 private int totalGuests;   

 //Joining Tables
 @ManyToOne
 @JoinColumn(name="passId")
 private Passenger passenger;

 @ManyToOne
 @JoinColumn(name="shipId")
 private Cruise cruise;

Cruise entity.

@Entity
@Table(name = "shipcruise")
public class Cruise implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer cruiseId;
private String cruiseName;
private LocalDate startDate;
private LocalDate endDate;
private Timestamp destination;

Passengers entity.

@Entity
@Table(name = "passengers")
public class Passenger implements Serializable{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer passengerId;
private String userName;
private String firstname;

In you code, you have:

 private int shipId;
 private int passId;

and below, you have:

  //Joining Tables
  @OneToOne
  @JoinColumn(name="passId")
  private Passenger passenger;

  @ManyToOne
  @JoinColumn(name="shipId")
  private Cruise cruise;

When, you do not use, @Column or @JoinColumn, the eclipse-link will use the field name, so, in this case, you will have 2 java properties that point to same column.

@JoinColumn does the dirty work for you(reference the other entity that references the SQL table), that is why we use JPA.

I changed the fields that represent primary key from int to Integer class. You can find why here: What's difference between primitive and wrapper class in JPA (Hibernate) column mappings?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=291515&siteId=1