I am working with Spring Data JPA/Hibernate and have two tables, one for orders and another one for customers. Each has a composite primary key as this is given by the DB model. The customer table is a general one that is used in multiple contexts and therefore has two columns "module" & "funtion" in the primary key. Now I want to join the customer in the orders entity with constant values for "module" and "function", which seems not to work with @JoinFormula..
orders entity:
@Getter
@Setter
@Entity
@Table(name = "customorder")
@IdClass(OrderId.class)
public class Order {
@Id
@Column("COMPANYNUMBER")
private Integer companyNumber;
@Id
@Column("CUSTOMERNUMBER")
private Integer customerNumber;
@Id
@Column("ORDERNUMBER")
private Integer orderNumber;
...
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(column = @JoinColumn(name = "COMPANYNUMBER", referencedColumnName = "COMPANYNUMBER", insertable = false , updatable = false)),
@JoinColumnOrFormula(column = @JoinColumn(name = "CUSTOMERNUMBER", referencedColumnName = "CUSTOMERNUMBER", insertable = false , updatable = false)),
@JoinColumnOrFormula(formula = @JoinFormula(value = "'MODULE_FOO'", referencedColumnName = "MODULE")),
@JoinColumnOrFormula(formula = @JoinFormula(value = "'FUNCTION_BAR'", referencedColumnName = "FUNCTION"))
})
private Customer customer;
}
Id class of orders entity:
public class OrderId implements Serializable {
private Integer companyNumber;
private Integer customerNumber;
private Integer orderNumber;
public OrderId() {
}
public OrderId(Integer companyNumber, Integer customerNumber, Integer orderNumber) {
super();
this.companyNumber = companyNumber;
this.customerNumber = customerNumber;
this.orderNumber = orderNumber;
}
}
Customer entity:
@Getter
@Setter
@Entity
@Table(name = "customer")
@IdClass(CustomerId.class)
public class Customer {
@Id
@Column("COMPANYNUMBER")
private Integer companyNumber;
@Id
@Column("CUSTOMERNUMBER")
private Integer customerNumber;
@Id
@Column("MODULE")
private String module;
@Id
@Column("FUNCTION")
private String function;
...
}
Id class of customer entity:
public class CustomerId implements Serializable {
private Integer companyNumber;
private Integer customerNumber;
private String module;
private String function;
public CustomerId() {
}
public CustomerId(Integer companyNumber, Integer customerNumber, String module, String function) {
super();
this.companyNumber = companyNumber;
this.customerNumber = customerNumber;
this.module = module;
this.function = function;
}
}
Beside of that I read the order via a custom native query in the corresponding Repository:
public interface OrderDao extends JpaRepository<Order, OrderId> {
@Query(value = "SELECT COMPANYNUMBER, CUSTOMERNUMBER, ORDERNUMBER, ... FROM customorder WHERE COMPANYNUMBER = :companyNumber", nativeQuery = true)
List<Order> findAllByCompanyNumber(@Param(value = "companyNumber") Integer companyNumber)
}
When I try to load the data via the repository method, I receive an excpetion that the sql could not be executed as an undefined column name was detected. In detail the constant columns module and function can not be resolved..
Any ideas, how to set up this constellation correctly?
Thanks for any help and ideas!
2条答案
按热度按时间tct7dpnv1#
I am not familiar with @JoinColumnOrFormula, but I implemented your entities and I noticed that your reference column name here should be 'MODULE' not 'MODUL'. Also you have a table named 'order' which is a reserved word in SQL - 'ORDER BY'
jqjz2hbq2#
I have found a quite simple solution meanwhile, where I have changed the @JoinColumns and the Query in the OrderDao:
inside orders entity:
inside order dao:
Maybe this will help someone in the future..