Hello guys,
I am testing JPA annotation with Hibernate, but lazy loading doesn't work in my case where the FK in Stock is marketCode, a CHAR(2), but marketCode is not a PK in StockMarket.
I have two entities:
Code:
@Entity
@Table(name = "stock")
public class Stock extends BaseObject {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
..
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "marketCode", referencedColumnName = "marketCode", nullable = false, unique = false, insertable = false, updatable = false)
private StockMarket stockMarket;
..
}
@Entity
@Table(name="stock_market")
public class StockMarket extends BaseObject {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
..
@OneToMany(mappedBy="marketCode", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = "marketCode", referencedColumnName = "marketCode")
private Set<Stock> stocks = new HashSet<Stock>(0);
..
}
Lazy loading works fine from StockMarket to Stock, but when I get Stock from DB, StockMarket will also be fetched.
Could anyone please point me to the right direction?
Thanks a lot in advance.