ObjectDB ObjectDB

How to apply constraints in collection attributes of entity.

#1

How to apply constraints like unique, not null ...etc in collection attribute of a entity.

Example :


@Entity

@Table(name = "agent")
public class Agent implements Serializable
{
   


@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long Id;

    private List<String> PhoneNumbers;

    public Agent()
    {
    }

    public Long getId()
    {
        return Id;
    }

    public List<String> getPhoneNumbers()
    {
        return PhoneNumbers;
    }

    public void setPhoneNumbers(List<String> PhoneNumbers)
    {
        this.PhoneNumbers = PhoneNumbers;
    }

}

in above example i wants to implement unique and notnull constraints on PhoneNumbers, means PhoneNumbers must not be duplicate or null How to do this?

 

Thanks

edit
delete
#2

For unique you can use JDO's Unique annotation.

For other constraints you can check objects in lifecycle events.

ObjectDB Support
edit
delete
#3

no my question was If i have PhoneNumbers["1234","567","45689"] in an record then "1234" should not be added in any other recoed's PhoneNumbers list. "1243" should be unique through out the Entity's PhoneNumbers.

Example:

if a entity exist like Agent:{"PhoneNumbers":["1234","5467","980"],"Id":1}

then Agent:{"PhoneNumbers":["1234","5467","980"],"Id":2} --- wrong

       Agent:{"PhoneNumbers":["1234","5467",null],"Id":3} ---- wrong

        Agent:{"PhoneNumbers":["345","9877","4500"],"Id":4} ---- correct

 

edit
delete
#4

You can use a unique path index.

Try:


@javax.jdo.annotations.Unique
private List<String> PhoneNumbers;
ObjectDB Support
edit
delete
#5

yes working thank you.

edit
delete

Reply

To post on this website please sign in.