I have created a simple 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 these tables:
AUTHOR table
* Author_ID, PK
* First_Name
* Last_Name
TITLES table
* TITLE_ID, PK
* NAME
* Author_ID, FK
TITLES table
* DOMAIN_ID, PK
* NAME
* TITLE_ID, FK
My version :) :
AUTHOR table
Code:
@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;
TITLES table[/size]
Code:
@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;
TITLES table
Code:
@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;
}
I don't think I made such a good job, especially for connecting the tables...
Any comments? :)