ormlite-core icon indicating copy to clipboard operation
ormlite-core copied to clipboard

Weak Entities Support

Open jpitchardu opened this issue 8 years ago • 2 comments

Let's say I've a Client and Seller entities, and both have an Address, which is unique for entity, currently the way to do this would be to have each of the fields of the Address on the entities like

public class Client {
    private String name;
    private String st1;
    private String st2;
 }

public class Seller {
    private String name;
    private String st1;
    private String st2;
}

It would be really, really good to be able to do something like:

public class Client {
    private String name;
    private Complex<Address> address;
}

public class Seller {
    private String name;
    private Complex<Address> address;
}

public class Address {
    private String st1;
    private String st2;
}

jpitchardu avatar Jan 17 '17 16:01 jpitchardu

I see no problems. Why you don't use the address as an entity? :-)

public abstract class AEntity {

    @DatabaseField(generatedId = true, columnName = "ID", canBeNull = false)
    private Integer id;

    // setter, getter
}

@DatabaseTable(tableName = "CLIENT")
public class Client extends AEntity {

    @DatabaseField(foreign = true, columnName = "ID_ADDRESS")
    private Address address;
    // setter, getter
}

@DatabaseTable(tableName = "SELLER")
public class Seller extends AEntity {

    @DatabaseField(foreign = true, columnName = "ID_ADDRESS")
    private Address address;
    // setter, getter
}

@DatabaseTable(tableName = "ADDRESS")
public class Address extends AEntity {

    @DatabaseField(columnName = "ST1")
    private String st1;
    @DatabaseField(columnName = "ST2")
    private String st2;

    // setter, getter
}

An equivalent of @Embeddable like hibernate doesn't exist for ORMLite.

Kcorab avatar Jul 21 '17 10:07 Kcorab

@Kcorab yeah that's one solution, but Address is considered a Weak entity, meaning that it doesn't have any sense to have an Address without a Seller, and a Seller can only have one Address, so the right way to map that relationship would be with address fields on the Seller and Client table

jpitchardu avatar Jul 22 '17 15:07 jpitchardu