[英]Hibernate mapping of internationalized database
我想根據休眠查詢中提供的語言環境從數據庫中獲取國際內容。 這是關於休眠映射的問題,但是如果我的錯誤,請隨時提出更好的數據庫設計。
我的數據庫設計(簡體): 數據庫設計因此,我有一個帶有不可翻譯數據的表,另一個帶有翻譯內容的表,但帶有用於區分語言的其他字段“ locale”。
我的java類看起來像這樣:
public class Car {
private Long id;
private Long length;
private Long weight;
private CarTranslated carTranslated;
// getters and setters
public class CarTranslated {
private Long id;
private Long carId;
private String desc;
// getters and setters
我希望能夠通過一次查詢獲得一輛汽車。 對於常規的jdbc,我將使用類似以下sql查詢的內容 :
public Car getById(Long id, Locale locale) {
Car c = new Car();
String sql = "select c.car_id, c.length, c.weight, ct.id, ct.descryption,
ct.car_id as "Translated car_id" from car c join car_translated ct on
(c.car_id = ct.car_id) where c.car_id ="+ id+" and ct.locale ='"+locale+"'";
// code to set fields of the object using ResultSet
return c;
}
休眠注釋映射和此設置的查詢是什么? 我嘗試了幾次,但無濟於事。 目前,我的最佳嘗試如下:
制圖:
@Entity
@Table(name="CAR")
public class Car {
@Id
@Column(name="car_id")
private Long carId;
@Column (name="weight")
private Long carWeight;
@Column (name="length")
private Long carLength;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name ="CAR_ID")
private CarTranslated localized;
// getters and setters
@Entity
@Table(name="CAR_TRANSLATED")
public class CarTranslated {
@Id
@Column (name="id")
private Long id;
@Column (name="car_id")
private Long carId;
@Column (name="descryption")
private String desc;
@Column(name="locale")
private Locale locale;
DAO:
public Car getCarById(Locale locale, Long id) {
Car car = new Car();
try {
Session session = HibernateUtils.getSessionFactory().openSession();
Criteria cr = session.createCriteria(Car.class)
.add(Restrictions.eq("carId", id));
Criteria cr1 = session.createCriteria(CarTranslated.class)
.add(Restrictions.eq("locale", locale));
car = (Car) cr.uniqueResult();
car.setLocalized((CarTranslated) cr1.uniqueResult());
} catch (Exception e) {
System.out.println(e.getMessage());
}
return car;
}
這是一種變通方法,我想知道什么是正確的方法?
映射到FK時,兩列上都應有一個注釋。 ( JavaDoc )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.