Ricardo Simões
Ricardo Simões

Reputation: 103

Complex Object as Inner Class inside Hibernate Entity

Now, I'm aware that an Inner class cannot be an Entity in Hibernate.

I'll first show my code, please refer to my question below:

@Entity
@Table(name = "bags")
public class Bags extends AbstractModel {

    private String brand;
    private String condition;
    private String size;
    private Extras extras;

    @ManyToOne
    private Customer customer;

    private class Extras {
        private boolean box;
        private boolean authenticity_card;
        private boolean shoulder_strap;
        private boolean dustbag;
        private boolean pouch;
        private boolean padlock_and_key;
        private boolean bagcharm;
        private boolean nameTag;
        private boolean mirror;
   }
}

Getters and setters are ommited. My question is:

If I want to have a slightly more complex object such as Extras, in which I represent the absence or not of several accessories, would it be better to create an additional table associated with bags OR is there a way around this?

Please let me know if I was not clear or you require additional information.

Upvotes: 0

Views: 164

Answers (1)

Oleksii Valuiskyi
Oleksii Valuiskyi

Reputation: 2851

@Embeddable annotation is used to declare a class will be embedded by other entities.

@Embeddable
public class Extras {
        private boolean box;
        private boolean authenticity_card;
        private boolean shoulder_strap;
        private boolean dustbag;
        private boolean pouch;
        private boolean padlock_and_key;
        private boolean bagcharm;
        private boolean nameTag;
        private boolean mirror;
}

@Embedded is used to embed a type into another entity.

@Entity
@Table(name = "bags")
public class Bags extends AbstractModel {

    private String brand;
    private String condition;
    private String size;
    private Extras extras;

    @ManyToOne
    private Customer customer;

    @Embedded
    private Extras extras;
}

Upvotes: 3

Related Questions