{code:java} package be.amaris.example.business.naturalid;
import java.util.Date;
import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table;
import org.hibernate.annotations.ForeignKey; import org.hibernate.annotations.NaturalId;
import be.amaris.frame.business.PersistentObjectByLong; import be.amaris.frame.infrastructure.IValidator;
@Entity @Table(name="EXA_FLAT_BOAT") @AttributeOverrides({ @AttributeOverride(name="id", column=@Column(name="FLB_ID", length=5)), @AttributeOverride(name="versionId", column=@Column(name="FLB_VRS")) }) public class FlatBoat extends PersistentObjectByLong{ private Barge barge; private String name;
@Override public void validate(IValidator validator) { } @ManyToOne(fetch=FetchType.LAZY) @JoinColumn(name = "BAR_ID", nullable=false) @ForeignKey(name = "EXAF_FLB_BAR_ID") @NaturalId(mutable=false) public Barge getBarge() { return barge; } @Column(name = "FLB_VALID_START", nullable = false) @NaturalId(mutable = true) public String getName() { return name; } public void setBarge(Barge barge) { this.barge = barge; } public void setName(String name) { this.name = name; } } {code}
{code:java} package be.amaris.example.business.naturalid;
import java.util.HashSet; import java.util.Set;
import javax.persistence.AttributeOverride; import javax.persistence.AttributeOverrides; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.OneToMany; import javax.persistence.OrderBy; import javax.persistence.Table;
import be.amaris.frame.business.PersistentObjectByLong; import be.amaris.frame.infrastructure.IValidator;
@Entity @Table(name="EXA_BARGE") @AttributeOverrides({ @AttributeOverride(name="id", column=@Column(name="BAR_ID", length=5)), @AttributeOverride(name="versionId", column=@Column(name="BAR_VRS")) }) public class Barge extends PersistentObjectByLong { @Override public void validate(IValidator validator) { } } {code}
{code:java} @Test public void testImmutableNaturalIdShouldBeChangeable testImmutableNaturalIdShouldNotBeChangeable () throws PersistenceExceptionWithDetail { // if there is another @NaturalId for which mutable = true, then the the @NaturalId with mutable = false can also be changed Barge first = new Barge(); bargeDAO.save(first);
FlatBoat flatBoat = new FlatBoat(); flatBoat.setBarge(first); flatBoat.setName("name"); flatBoatDAO.save(flatBoat); persistenceManager.commit(); Barge second = new Barge(); bargeDAO.save(second);
flatBoat.setBarge(second); try { persistenceManager.commit(); fail(); } catch (PersistenceExceptionWithDetail e) { assertEquals(PersistenceExceptionWithDetail.ERROR_IMMUTABLE_NATURALID, e.getMessage()); } } {code}
|
|