我有一个类ContactDTO
的JPA继承,UserDTO
使用 Single_Table 策略进行扩展:
package ch.ffhs.cryptomess.server.dto;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import org.springframework.transaction.annotation.Transactional;
@Transactional
@Entity(name = "ch.ffhs.cryptomess.server.dto.ContactDTO")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue(value = "ContactDTO")
@Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = {
"username", "discriminator" }))
public class ContactDTO implements Serializable {
private static final long serialVersionUID = 7706720499337697645L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "user_id_seq")
@SequenceGenerator(name = "user_id_seq", allocationSize = 1, initialValue = 10)
@Column(name = "id")
protected Long id;
@Version
protected Long version;
@Column(name = "public_key", length = 255)
protected String publicKey;
@Id
@Column(length = 50, updatable = false)
protected String username;
@ManyToMany(mappedBy = "contacts", targetEntity = CommunicationDTO.class)
protected Set<CommunicationDTO> communications = new HashSet<CommunicationDTO>(
0);
@Override
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final ContactDTO other = (ContactDTO) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
public Set<CommunicationDTO> getCommunications() {
return communications;
}
public Long getId() {
return id;
}
public String getPublicKey() {
return publicKey;
}
public String getUsername() {
return username;
}
public Long getVersion() {
return version;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((username == null) ? 0 : username.hashCode());
return result;
}
public void setCommunications(final Set<CommunicationDTO> communications) {
this.communications = communications;
}
public void setId(final Long id) {
this.id = id;
}
public void setPublicKey(final String publicKey) {
this.publicKey = publicKey;
}
public void setUsername(final String username) {
this.username = username;
}
public void setVersion(final Long version) {
this.version = version;
}
}
和
package ch.ffhs.cryptomess.shared.dto;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import org.springframework.transaction.annotation.Transactional;
import ch.ffhs.cryptomess.server.dto.ContactDTO;
import com.google.gwt.view.client.ProvidesKey;
@Transactional
@Entity
@DiscriminatorValue(value = "UserDTO")
public class UserDTO extends ContactDTO implements Serializable {
private static final long serialVersionUID = -3078472527069117428L;
@Column(length = 80)
protected String password;
@Column(name = "is_account_non_expired", columnDefinition = "BIT")
protected boolean isAccountNonExpired;
@Column(name = "is_account_locked", columnDefinition = "BIT")
protected boolean isAccountLocked;
@Column(name = "is_credentials_non_expired", columnDefinition = "BIT")
protected boolean isCredentialsNonExpired;
@Column(name = "is_disabled", columnDefinition = "BIT")
protected boolean isDisabled;
/**
* The key provider that provides the unique ID of a contact.
*/
public static final ProvidesKey<UserDTO> KEY_PROVIDER = new ProvidesKey<UserDTO>() {
@Override
public Object getKey(final UserDTO item) {
return item == null ? null : item.getId();
}
};
public String getPassword() {
return password;
}
public boolean isAccountLocked() {
return isAccountLocked;
}
public boolean isAccountNonExpired() {
return isAccountNonExpired;
}
public boolean isAccountNonLocked() {
return !isAccountLocked;
}
public boolean isCredentialsNonExpired() {
return isCredentialsNonExpired;
}
public boolean isDisabled() {
return isDisabled;
}
public boolean isEnabled() {
return !isDisabled;
}
public void setAccountLocked(final boolean isAccountLocked) {
this.isAccountLocked = isAccountLocked;
}
public void setAccountNonExpired(final boolean isAccountNonExpired) {
this.isAccountNonExpired = isAccountNonExpired;
}
public void setAccountNonLocked(final boolean isAccountNonLocked) {
this.isAccountLocked = !isAccountNonLocked;
}
public void setCredentialsNonExpired(final boolean isCredentialsNonExpired) {
this.isCredentialsNonExpired = isCredentialsNonExpired;
}
public void setDisabled(final boolean isDisabled) {
this.isDisabled = isDisabled;
}
public void setEnabled(final boolean isEnabled) {
this.isDisabled = !isEnabled;
}
public void setPassword(final String password) {
this.password = password;
}
}
如果我在UserDTO
- enity上进行合并或创建,我希望在数据库中创建/更新ContactDTO
- 实体。有没有办法实现这一点,而不单独处理这两个实体?
感谢您的帮助!