Unexpected behavior in generation @Onetomany unidirectional (additional FK columns being created)

Asked

Viewed 69 times

1

Below follows the two entities with unidirectional relationships:

Customerservice.java

@Entity
@Table(name = "customer_service")
@Multitenant
@TenantDiscriminatorColumn(name = "tenant_id", discriminatorType = DiscriminatorType.INTEGER, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
public class CustomerService implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_customer_service")
    private Long id;

    @Column(name = "tenant_id", insertable = false, updatable = false)
    private Long tenantId;

    @Column(name = "date_service")
    @DateTimeFormat(pattern = "dd/MM/yyyy")
    @Temporal(TemporalType.DATE)
    private Date date;

    @Column(name = "average_budget")
    private BigDecimal averageBudget;

    @Column(name = "service_situation")
    private boolean situation;

    @OneToMany
    @JoinColumn(name="fk_customerService")
    private Set<ServiceItem> serviceItem;

    //Getter and Setter...
}

Destination.java

@Entity
@Table(name="destination")
@Multitenant
@TenantDiscriminatorColumn(name="tenant_id", discriminatorType=DiscriminatorType.INTEGER, contextProperty=PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT) 
public class Destination implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id_destination")
    private Long idDestination;

    @Column(name="tenant_id", insertable=false, updatable=false)
    private Long tenantId;

    @OneToMany(cascade={CascadeType.PERSIST, CascadeType.MERGE}, fetch=FetchType.EAGER)
    @JoinColumn(name="fk_destination")
    @Valid
    private Set<Image> images;

    @OneToMany
    @JoinColumn(name="fk_destination")
    private Set<ServiceItem> serviceItem;

    //Geter and Setter...
}

Serviitem.java

@Entity
@Table(name = "service_item")
@Multitenant
@TenantDiscriminatorColumn(name = "tenant_id", discriminatorType = DiscriminatorType.INTEGER, contextProperty = PersistenceUnitProperties.MULTITENANT_PROPERTY_DEFAULT)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")
public class ServiceItem implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_service_item")
    private Long id;


    private Destination destination;


    private CustomerService customerService;

    //Getter and Setter
    }

Well, the generation of tables in BD is done by eclipselink, but the same is creating (besides the columns fk_customerService and fk_destination), Two more columns like FK! Below follows an image to illustrate the problem: imagem

How can I fix this generation?

  • 1

    The relationship is not unidirectional, since you have the attributes Destination and CustomerService in ServiceItem. This seems like a bug, but anyway I think it’s worth trying to define the annotations @JoinColumn in class ServiceItem instead of the others.

  • @utluiz, I will fix it. Lack of attention to mine. But if I set the notes of @JoinColumn in ServiceItem will not make the relationship bi-directional?

  • 1

    Yeah, but it’s just a test to see if it solves the problem of FK duplication. Another test would be to temporarily remove these attributes to see if Eclipselink is considering themselves without the annotations.

  • Cool @utluiz. I will perform the tests!

No answers

Browser other questions tagged

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