BAEL-2901 Composite primary keys in JPA (#6994)
* BAEL-2901 Composite primary keys in JPA * Formatted code using the recommended formatter.xml
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
|
||||
@Entity
|
||||
@IdClass(AccountId.class)
|
||||
public class Account {
|
||||
|
||||
@Id
|
||||
private String accountNumber;
|
||||
|
||||
@Id
|
||||
private String accountType;
|
||||
|
||||
private String description;
|
||||
|
||||
public String getAccountNumber() {
|
||||
return accountNumber;
|
||||
}
|
||||
|
||||
public void setAccountNumber(String accountNumber) {
|
||||
this.accountNumber = accountNumber;
|
||||
}
|
||||
|
||||
public String getAccountType() {
|
||||
return accountType;
|
||||
}
|
||||
|
||||
public void setAccountType(String accountType) {
|
||||
this.accountType = accountType;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class AccountId implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String accountNumber;
|
||||
private String accountType;
|
||||
|
||||
public AccountId() {
|
||||
|
||||
}
|
||||
|
||||
public AccountId(String accountNumber, String accountType) {
|
||||
this.accountNumber = accountNumber;
|
||||
this.accountType = accountType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((accountNumber == null) ? 0 : accountNumber.hashCode());
|
||||
result = prime * result + ((accountType == null) ? 0 : accountType.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
AccountId other = (AccountId) obj;
|
||||
if (accountNumber == null) {
|
||||
if (other.accountNumber != null)
|
||||
return false;
|
||||
} else if (!accountNumber.equals(other.accountNumber))
|
||||
return false;
|
||||
if (accountType == null) {
|
||||
if (other.accountType != null)
|
||||
return false;
|
||||
} else if (!accountType.equals(other.accountType))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
|
||||
import javax.persistence.EmbeddedId;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@EmbeddedId
|
||||
private BookId bookId;
|
||||
|
||||
private String description;
|
||||
|
||||
public Book() {
|
||||
|
||||
}
|
||||
|
||||
public Book(BookId bookId) {
|
||||
this.bookId = bookId;
|
||||
}
|
||||
|
||||
public BookId getBookId() {
|
||||
return bookId;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.baeldung.jpa.entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.Embeddable;
|
||||
|
||||
@Embeddable
|
||||
public class BookId implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private String title;
|
||||
private String language;
|
||||
|
||||
public BookId() {
|
||||
|
||||
}
|
||||
|
||||
public BookId(String title, String language) {
|
||||
this.title = title;
|
||||
this.language = language;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getLanguage() {
|
||||
return language;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((language == null) ? 0 : language.hashCode());
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
BookId other = (BookId) obj;
|
||||
if (language == null) {
|
||||
if (other.language != null)
|
||||
return false;
|
||||
} else if (!language.equals(other.language))
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user