简体   繁体   中英

What Hibernate annotations am I missing?

I'm brand new to Hibernate, and I'm trying to get a fairly simple Hibernate code snippet to work. After reading the tutorials, I'm totally choking on the full implementation.

For one, when it comes to the hbm2ddl.auto property, I'm setting it to validate because I just don't like the idea of Hibernate creating my table structure (I'm old fashioned; perhaps that will change as I become more comfortable with Hibernate though). In any event, here's the table I just created on a MySQL server:

CREATE TABLE users (
    id                      INT NOT NULL AUTO INCREMENT,
    email                   VARCHAR(200) NOT NULL,
    title                   VARCHAR(25),
    first_name              VARCHAR(100),
    middle_name             VARCHAR(100),
    last_name               VARCHAR(100),
    suffix                  VARCHAR(100),

    PRIMARY KEY (id)
);

Thise corresponds to the following POJOs/entities in my app's code:

@Entity
@Table(schema="my_db", name="users")
public class User {
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="id")
    private Integer id;

    @Column(name="email")
    private String email;

    // ???
    private PersonName personName;

    public User(final String email, final PersonName personName) {
        super();

        setEmail(email);
        setPersonName(personName);
    }

    // Getters and setters omitted for brevity...
}

public abstract class BaseName {
    public abstract String toName();

    @Override
    public String toString() {
        return toName();
    }
}

public class PersonName extends BaseName {
    private String title;
    private String firstName;
    private String middleName;
    private String lastName;
    private String suffix;

    public PersonName(final String title, final String firstName, final String middleName, final String lastName, final String suffix) {
        super();

        setTitle(title);
        setFirstName(firstName);
        setMiddleName(middleName);
        setLastName(lastName);
        setSuffix(suffix);
    }

    // Getters and setters omitted for brevity...
}

What annotations/config do I need to add so that User#personName gets persisted as an embedded PersonName object inside the users table? In other words, User is an entity and contains a PersonName as an embedded objects (non-entity).

Also, any other obvious annotations I'm missing? Thanks in advance!

You can check the title 2.2.2.4. Embedded objects (aka components) 2.2.2.4. Embedded objects (aka components) in docs:

http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#entity-mapping-property

As suggested by user @Snow Blind, Embedded objects are what you want.

Let's start with class PersonName :

@Embeddable
public class PersonName extends BaseName {
    @Column(name = "title")
    private String title;
    @Column(name = "first_name")
    private String firstName;
    @Column(name = "middle_name")
    private String middleName;
    @Column(name = "last_name")
    private String lastName;
    @Column(name = "suffix")
    private String suffix;

    // ...
}

Now add the @Embedded tag here:

// ...

@Embedded
private PersonName personName;

// ...

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM