Hello everybody, if anybody could tell me how to organize logical constraints at Hibernate side for relations between models. The problem is that we dont have phisicaly it at database side and should not create them. We have 2 entities: Shop and City, Shop have not nullable City If user removes City, which affiliate to some Shop, it not give any exception , but when we want preview this shop later - we take exception because hibernate cant find required city which was deleted.
I wonder, is any possibility to warn user that City which is trying to remove has relations with some Shop
Here are code for detail: @SuppressWarnings("unchecked") @Entity @Table(name = "T_SHOP") public class Shop extends BaseObject implements java.io.Serializable { private Long shopId; private City city; private String address; ... @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "CITY", nullable = false) public City getCity() { return city; }
public void setCity(City city) { this.city = city; } ... }
@Entity @Table(name = "T_CITY") public class City extends BaseObject implements java.io.Serializable { private Long cityId; private String cityName; ... @SuppressWarnings("unchecked") private List<Shop> shops = LazyList.decorate(new ArrayList<Shop>(), FactoryUtils.instantiateFactory(Shop.class)); ... @OneToMany(fetch=FetchType.LAZY,cascade = { CascadeType.PERSIST },mappedBy="city") @Fetch(FetchMode.SELECT) public List<Shop> getShops() { return shops; }
public void setShops(List<Shops> shops) { this.shops = shops; } } At DB constraints are absent phisically, and I should not create them (!) . It is possible to make some annotations at model that Hibernate checks this constraints at own logic ?
Thanks
|