I got the following classes I want to save in the DB with Ormlite:
public class Quantity {
private int normalisedAmount;
private String unit;
....
}
public class Article {
private String name;
private Quantity q;
....
}
The thing is that there is a one-to-one relationship between Quantity and Article. One way to use Ormlite is to make q foreign like:
@DatabaseField(foreign = true)
private Quantity q;
The drawback is that the DB will not be normilazed.
One other solution is to make a Custom Data Type by create a Persister. The problem there is that you either have to serialize the Quantity object or put the fileds “name” and “q” into the same DB column. None of them looks nice.
How do I make a nice looking solotion?
To get one-to-one relationship, you could you put a
uniqueconstraint on theqfield. I’ve not tried it but it should limit the number of rows with theq.namefield to 1.This will create a
Quantitytable which (I believe) is how other ORMs do it. I don’t think this violates database “normalization”. YourArticletable has a single field in it which represents the id of theQuantity— not sure what the id field is there. All of the rest of the fields fromQuantitywill be in the other table.The alternative would be to embed
QuantityintoArticlewhich ORMLite does not support.