Java-Hibernate: How can I translate these tables to hibernate annotations?

Posted by penas on Stack Overflow See other posts from Stack Overflow or by penas
Published on 2010-04-10T17:50:03Z Indexed on 2010/04/10 17:53 UTC
Read the original article Hit count: 236

Filed under:

I need to create a simple application using these tables: http://stackoverflow.com/questions/2612848/are-these-tables-respect-the-3nf-database-normalization

I have created the application using simple old JDBC, but I would like to see how the application would look like using Hibernate, but I don't know how to put the sql code in java. I have found LOTS of examples, but I'm pretty much confused about using Hibernate and I don't know If I made such a good joob.

For example, for the first three tables:

AUTHOR table

* Author_ID, PK
* First_Name
* Last_Name

TITLES table

* TITLE_ID, PK
* NAME
* Author_ID, FK

DOMAIN table

* DOMAIN_ID, PK
* NAME
* TITLE_ID, FK

The code in java:

Table 1

   @Entity
    @Table(name = "AUTHORS", schema = "LIBRARY")
    public class Author{
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "Author_ID")
        private int authorId;
        @Column(name = "First_Name", nullable = false, length = 50)
        private String firstName;
        @Column(name = "Last_Name", nullable = false, length = 40)
        private String lastName;
        @OneToMany
        @JoinColumn(name = "Title_ID")
        private List<Title> titles;

Table 2

@Entity
@Table(name = "TITLES")
public class Title{

    @Id
    @Column(name = "Title_ID")
    private int titleID;
    @Column(name = "Name", nullable = false, length = 50)
    private String name;

    @ManyToOne
    @JoinColumn(name = "Domain_ID")
    private Domain domains;

Table 3

@Entity
@Table(name = "DOMAINS")
public class Domain{
      @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "Domain_ID")
        private int Domain_ID;
        @Column(name = "Name", nullable = false, length = 50)
        private String name;

        @OneToOne(mappedBy = "domains")
        private Title title;
}

Any good? :)

© Stack Overflow or respective owner

Related posts about hibernate