Error in mappedBy when making 2 user relationship

Asked

Viewed 68 times

-2

i have two classes one of user and one of request, my user class has roles and in the order class I want to know who was the employee and the client who made the request and am having this error mappedBy Reference an Unknown target Entity Property.

User class:

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    private String username;
    
    @JsonIgnore
    @NotEmpty
    private String password;
    
    @Email
    private String email;
    
    private BigDecimal balance;
    
    @JsonIgnore
    @OneToMany(mappedBy = "user")
    private List<Order> orders = new ArrayList<>();

    @ElementCollection
    @CollectionTable(name="TELEFONE")
    private Set<String> telefones = new HashSet<>();
    
    @ElementCollection(fetch=FetchType.EAGER)
    @CollectionTable(name="ROLES")
    private Set<Integer> roles = new HashSet<>();

Order class

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   
   @ManyToOne
   @JoinColumn(name = "user_id")
   private User costumer;
   
   @ManyToOne
   @JoinColumn(name = "user_id")
   private User employer;
   
   private Payment payment;
   
   @OneToMany(mappedBy = "id.order")
   private Set<ItemOrder> items = new HashSet<>();
   
   @JsonFormat(pattern="dd/MM/yyyy HH:mm")
   private LocalDateTime date;

I have no idea what I can do to fix this, I’m learning and I’d like to know how to fix it.

  • Could describe the situation better, your mapping seems to have more than one problem. If it is possible to include a class diagram

1 answer

0

The problem you are experiencing is the following:

You mapped @Onetomany wrong.

@JsonIgnore
@OneToMany(mappedBy = "costumer", insertable='false', updatable='false')
private List<Order> ordersCustomer = new ArrayList<>();

@JsonIgnore
@OneToMany(mappedBy = "employer", insertable='false', updatable='false')
private List<Order> ordersEmployer = new ArrayList<>();

Mapping in this way means that a person has products linked to both the consumer and the employee.

insertable and updatable false means that you cannot insert "Order" objects via setters with the "User" class".

Another error is this:

   @ManyToOne
   @JoinColumn(name = "user_id")
   private User costumer;
   
   @ManyToOne
   @JoinColumn(name = "user_id")
   private User employer;

Think in terms of the bank, what is the name of each column?

Maybe I’ll stay that way:

   @ManyToOne
   @JoinColumn(name = "cod_costumer")
   private User costumer;
   
   @ManyToOne
   @JoinColumn(name = "cod_employer")
   private User employer;

I suggest to see the documentation of Hibernate. https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/OneToMany.html https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/ManyToOne.html

Browser other questions tagged

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