Reputation: 23
I have a one-to-many relationship with Customer and Address, but I'm not sure how to represent this with JPA. I don't want use @OneToMany for the AddressEntity's in CustomerEntity because I want to avoid wrapping it in a Collection.
I'm wondering what annotation or even other strategies I can use to maintain the relationship where one customer will, for simplicity, always have two addresses. Any suggestions are appreciated!
Address Entity
@Data
@NoArgsConstructor
@Entity(name = "address")
public class AddressEntity
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@?
private CustomerEntity customer;
}
Customer Entity
@Data
@NoArgsConstructor
@Entity(name = "customer")
public class CustomerEntity
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@?
private AddressEntity shippingAddress;
@?
private AddressEntity billingAddress;
}
Upvotes: 1
Views: 1870
Reputation: 19956
For the case when an address can belong different customers.
@Entity
public class AddressEntity
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
}
@Entity
public class CustomerEntity
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private AddressEntity shippingAddress;
@ManyToOne
private AddressEntity billingAddress;
}
if each customer has unique address, better to store the addresses in the same customer record.
You can create class EmbeddedAddress
and use @Embedded
and @Embeddable
annotations.
Upvotes: 1
Reputation: 119
For your exact scenario, I think you could go for @PostLoad. The steps would be:
@OneToMany
annotation to load the addresses into a collectionshippingAddress
and billingAddress
with @Transient@PostLoad
Why would the steps above work?
PostLoad
is invoked after an entity is loaded from the databaseA relevant example can be found here.
While the approach above would solve your problem, it adds some degree of verbosity in your JPA entities. I would suggest to go for @OneToMany and make sure you add an enum in AddressEntity
to check if an address is for shipping or billing.
Also, given that you mentioned that there is a one-to-many relationship between a customer and an address, then there is a many-to-one relationship between an address and a customer. The annotation to use in the AddressEntity
class is @ManyToOne
Upvotes: 1