在 activeJDBC 中,如何强制存在与模型的一对多关系
In activeJDBC, how to enforce the presence of a one-to-many relationship to a model
假设我有以下数据库模型:
Car(
id INT
plaque_id INT
)
Plaque(
id INT
identification TEXT
)
所以在 ActiveJDBC 中,我的模型是:
public class Car extends Model {
static{
validatePresenceOf("plaque_id");
}
public Car () {}
}
..
public class Plaque extends Model {
static{
validatePresenceOf("identification");
}
public Car () {}
}
假设我的规格说明:汽车必须有牌匾。
如您所见,我强制 Car 模型存在 plaque_id。
现在。当我尝试这个时:
Car model_s = new Car();
Plaque plaque_a = new Plaque();
plaque_a.set("identification","A-8000");
plaque_a.saveIt();
car.add(plaque_a);
car.saveIt();
我抛出了以下异常:
java.lang.IllegalArgumentException: You can only add associated model
to an instance that exists in DB. Save this instance first, then you
will be able to add dependencies to it.
如果我没理解错的话,我的车model_s一定要先保存才能link牌匾plaque_a。但是由于我的验证规则,我无法在没有牌匾的情况下保存 model_s。这是第 22 条军规。
注意:我是 activeJDBC 新手。
我想你搞反了。由于你的tableCar
有一列plaque_id
,也就是说一个Plaque
有很多Car
(s),这是一对多的关联:http://javalite.io/one_to_many_associations。
因此,您需要将 Car
添加到 Plaque
,而不是相反:
Car model_s = new Car(); // set parameters on the car
plaque_a.set("identification","A-8000");
plaque_a.saveIt();
plaque_a.add(model_s);
其他推荐:
1) 在 Java 中,使用 CamelCase:modelS
,而不是 model_s
2) 添加构造函数到 Plaque
:
public class Plaque{
public Plaque(String identification){
set("identification", identification);
}
public Plaque(){} // must have default constructor
}
那么你的代码看起来会更干净:
Car model_s = new Car(); // set parameters on the car
Plaque plaque = new Plaque("A-8000");
plaque_a.saveIt();
plaque_a.add(modelS);
一般来说,尽量避免使用动态 setter 和 getter,它们对于小项目来说没问题,但是 writing permanent setters and getters 会给你带来惊人的 Java 重构能力,这是你在 Ruby。
假设我有以下数据库模型:
Car(
id INT
plaque_id INT
)
Plaque(
id INT
identification TEXT
)
所以在 ActiveJDBC 中,我的模型是:
public class Car extends Model {
static{
validatePresenceOf("plaque_id");
}
public Car () {}
}
..
public class Plaque extends Model {
static{
validatePresenceOf("identification");
}
public Car () {}
}
假设我的规格说明:汽车必须有牌匾。
如您所见,我强制 Car 模型存在 plaque_id。
现在。当我尝试这个时:
Car model_s = new Car();
Plaque plaque_a = new Plaque();
plaque_a.set("identification","A-8000");
plaque_a.saveIt();
car.add(plaque_a);
car.saveIt();
我抛出了以下异常:
java.lang.IllegalArgumentException: You can only add associated model to an instance that exists in DB. Save this instance first, then you will be able to add dependencies to it.
如果我没理解错的话,我的车model_s一定要先保存才能link牌匾plaque_a。但是由于我的验证规则,我无法在没有牌匾的情况下保存 model_s。这是第 22 条军规。
注意:我是 activeJDBC 新手。
我想你搞反了。由于你的tableCar
有一列plaque_id
,也就是说一个Plaque
有很多Car
(s),这是一对多的关联:http://javalite.io/one_to_many_associations。
因此,您需要将 Car
添加到 Plaque
,而不是相反:
Car model_s = new Car(); // set parameters on the car
plaque_a.set("identification","A-8000");
plaque_a.saveIt();
plaque_a.add(model_s);
其他推荐:
1) 在 Java 中,使用 CamelCase:modelS
,而不是 model_s
2) 添加构造函数到 Plaque
:
public class Plaque{
public Plaque(String identification){
set("identification", identification);
}
public Plaque(){} // must have default constructor
}
那么你的代码看起来会更干净:
Car model_s = new Car(); // set parameters on the car
Plaque plaque = new Plaque("A-8000");
plaque_a.saveIt();
plaque_a.add(modelS);
一般来说,尽量避免使用动态 setter 和 getter,它们对于小项目来说没问题,但是 writing permanent setters and getters 会给你带来惊人的 Java 重构能力,这是你在 Ruby。