Java tutorial
/* * Copyright 2015 Kaiserpfalz EDV-Service, Roland T. Lichti * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package de.kaiserpfalzEdv.piracc.backend.db.master; import de.kaiserpfalzEdv.piracc.backend.db.DatabaseSchemaNames; import de.kaiserpfalzEdv.vaadin.backend.HasCommonName; import de.kaiserpfalzEdv.vaadin.backend.HasId; import de.kaiserpfalzEdv.vaadin.backend.db.AuditedBaseEntity; import javax.persistence.Cacheable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import java.io.Serializable; import java.util.Set; import static javax.persistence.CascadeType.ALL; import static org.apache.commons.lang3.StringUtils.isNotBlank; /** * @author klenkes * @version 2015Q1 * @since 12.09.15 09:00 */ @Entity @Cacheable @Table(name = "IDENTITIES", catalog = DatabaseSchemaNames.master) public class Identity extends AuditedBaseEntity implements HasId, HasCommonName, Serializable { private static final long serialVersionUID = -6596096161145583874L; private String surName = ""; private String givenName = ""; private String commonName = ""; private Organization organization; private Long memberNumber; private boolean employee = false; private Set<CommunicationAddress> addresses; @Column(name = "sur_name_", nullable = false) @Size(min = 1, max = 100) public String getSurName() { return surName; } public void setSurName(String surName) { this.surName = surName; setCommonName(surName, givenName); } @Column(name = "given_name_", nullable = false) @Size(min = 1, max = 100) public String getGivenName() { return givenName; } public void setGivenName(String givenName) { this.givenName = givenName; setCommonName(surName, givenName); } @ManyToOne(fetch = FetchType.EAGER, optional = false) @JoinColumn(name = "organization_", nullable = false) @NotNull public Organization getOrganization() { return organization; } public void setOrganization(Organization organization) { this.organization = organization; } @Column(name = "member_number_") public Long getMemberNumber() { return memberNumber; } public void setMemberNumber(Long memberNumber) { this.memberNumber = memberNumber; } @Column(name = "employee_", columnDefinition = "TINYINT(1)", length = 1) public boolean getEmployee() { return employee; } public void setEmployee(boolean employee) { this.employee = employee; } @Override @Column(name = "common_name_", length = 100, nullable = false) @Size(min = 1, max = 100) public String getCommonName() { return commonName; } public void setCommonName(final String name) { this.commonName = name; } private void setCommonName(final String surName, final String givenName) { setCommonName((isNotBlank(surName) ? surName : "?") + ", " + (isNotBlank(givenName) ? givenName : "?")); } @OneToMany(fetch = FetchType.EAGER, cascade = { ALL }, orphanRemoval = true) @JoinColumn(name = "identity_", nullable = false) public Set<CommunicationAddress> getAddresses() { return addresses; } public void setAddresses(Set<CommunicationAddress> addresses) { this.addresses = addresses; } }