简体   繁体   中英

MappingException: broken column mapping during mapping one to one with joinTable

I explain the Global context. I want to create a simple system composed of two entities, an agency and employee. I want to model the notion of agency manager. Indeed, an agency with one and only one manager who is an employee of the agency. An employee can only be the manager of one and only one agency. However, not all employees are managers. There is only one employee who is a manager. To model this relationship. I thought of a OneToOne relationship managed by a join table created like this.

Database tables

Agency

create table agency (id bigint not null, creationDate datetime, numberOfEmployee integer, reference varchar(255), primary key (id))

Employe

create table employee (Id bigint not null, birthDate datetime, firstName varchar(255), gender integer, name varchar(255), socialSecurityNumber varchar(255), primary key (Id))

AgencyManager

create table agency_manager (employee_id bigint not null, agency_id bigint not null, primary key (agency_id, employee_id))

alter table agency_manager add constraint FKpod2iqjjd7s757fpdrk4f5fcb foreign key (employee_id) references employee (Id)

alter table agency_manager add constraint FK6mfhp2sc5ntk1grf8kqhyq4we foreign key (agency_id) references agency (id)

    @Entity
    @Table(name = "agency")
    public class Agency {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        Long id;
        String reference;
        Integer numberOfEmployee;
        Calendar creationDate;
        @OneToOne(mappedBy = "manager")
         // @Transient
        AgencyManager agencyManager;
    
    }



@Entity
@Table(name = "employee")
public class Employee {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Long Id;
    String name;
    String firstName;
    Gender gender;
    Calendar birthDate;
    String socialSecurityNumber;

    @OneToOne(mappedBy = "agency")
    //@Transient
    AgencyManager managedAgency;

    public boolean isAgencyManager(){
        return managedAgency!=null;
    }
}


@Entity
@Table(name = "agency_manager")
public class AgencyManager {
    @EmbeddedId
    AgencyManagerPK id;

    @OneToOne
    @JoinColumn(name = "agency_id",insertable = false,updatable = false)
    Employee manager;

    @ManyToOne
    @JoinColumn(name = "employee_id",insertable = false,updatable = false)
    Agency agency;
}


@Embeddable
public class AgencyManagerPK implements Serializable {

    @Serial
    private static final long serialVersionUID = 7474913724940112916L;
    @OneToOne
    @JoinColumn(name = "employee_id")
    Employee manager;

    @ManyToOne
    @JoinColumn(name = "agency_id")
    Agency agency;
}

my problem with this mapping is that i have an error org.hibernate.MappingException: broken column mapping for: agencyManager.id of: org.tuto.persistence.entity.Agency

by adding @Transient on the relations (agencyManager in Agency and managedAgency in Employee) the error disappears. However with the @Transient annotation on the relations, hibernate can no longer retrieve them for me. But I do want a mapping that will allow me to retrieve the entities with the relationship and also be able to persist my Agency and Employee entities separately without the AgnecyManager relationship.At the end, i want to be able to do something like this.

//i use lombok @Builder

 Employee employee=Employee.builder().name("Simple").firstName("Employee").socialSecurityNumber("112345").gender(Gender.MALE).build(); Agency agency = Agency.builder().reference("ONLY8976GYT7").numberOfEmployee(39).build(); EntityManager em = JpaUtils.getEmF().createEntityManager(); em.getTransaction().begin(); em.persist(agency); em.persist(employee); AgencyManagerPK agencyManagerPK = AgencyManagerPK.builder().manager(employee).agency(agency). build(); AgencyManager agencyManager= AgencyManager.builder().id(agencyManagerPK).build(); employee.setManagedAgency(agencyManager); agency.setAgencyManager(agencyManager); em.persist(agencyManager); em.getTransaction().commit();

I can't. Help me please

You have to use @OneToOne(mappedBy = "agency") in the Agency entity and @OneToOne(mappedBy = "employee") in the Employee entity. The mappedBy member refers to the name of the attribute in the target type of the association AgencyManager , through which the association is defined.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM