Q:

Adding foreign key field to a javabean

Consider this regular javabean without ORM/:
// primary key is auto incremented by the database, so I can't add it
public class User {
    String name;
    int personID; // foreign key
    // no args constructor
    // getter/setters for fields
}

Is it ok to do this? I personally think it doesn't make sense because there is no reason to manipulate the foreign key through the bean, but I might be wrong. Are there use cases where this is good?

A:

I would normally do it like this.
public class Person {

    private final String id;

    public Person(String id) {
        this.id = id;
    }

    public Person() {
        this.id = UUID.randomUUID().toString();
    }

}

and
public class User {

    private final String id;
    private String personId;

    public User(String id, String personId) {
        this.id = id;
        this.personId = personId;
    }

    public User() {
        this.id = UUID.randomUUID().toString();
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(String personId) {
        this.personId = personId;
    }

}

Another alternative for User is to make it immutable.  In which case it would look something like this.
public class User {

    private final String id;
    private final String personId;

    public User(String id, String personId) {
        this.id = id;
        this.personId = personId;
    }

    public User(String personId) {
        this.personId = personId;
        this.id = UUID.randomUUID().toString();
    }

    public String getPersonId() {
        return personId;
    }

}

Now the classes can be used in an autoincremented or non-autoincremented way.  Basically the option is there for the classes to make their own unique ID or for a unique ID to be passed to it.  
One common dilemma that I have seen is when the id does not exist yet the object does.  That is the case when the object is created in the program but the ID (which maybe created by the DB when it is inserted) is not assigned to it yet.  In that case the bean may look like this.
public class User {

    private String id;
    private final String personId;

    public User(String id, String personId) {
        this.id = id;
        this.personId = personId;
    }

    public User(String personId) {
        this.personId = personId;
    }

    public String getPersonId() {
        return personId;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

Notice the id is not final and hence the object can exist without the id present and can be added in later if needed

