patternjavaMinor
Hibernate insert with foreign key
Viewed 0 times
insertwithforeignhibernatekey
Problem
Ι have the following database:
and the following classes:
```
@Entity
@Table(name="Books"
,catalog="Autor"
)
public class Books implements java.io.Serializable {
private Integer bookId;
private Authors authors;
private String name;
public Books() {
}
public Books(Authors authors, String name) {
this.authors = authors;
this.name = name;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="BookID", unique=true, nullable=false)
public Integer getBookId() {
return this.bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
@ManyToOne
@JoinColumn(name="AutID", nullable=false,insertable = false, updatable = false)
public Authors getAuthors() {
return this.authors;
}
public void setAuthors(Authors authors) {
this.authors = authors;
}
@Column(name="Name", nullable=false, length=50)
public String getName() {
return this.name;
}
public void setName(String name) {
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `Authors` (
`AutID` int(11) NOT NULL,
`Name` varchar(50) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
INSERT INTO `Authors` (`AutID`, `Name`) VALUES
(1, 'Tolkien');
CREATE TABLE IF NOT EXISTS `Books` (
`BookID` int(11) NOT NULL,
`AutID` int(11) NOT NULL,
`Name` varchar(50) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
ALTER TABLE `Authors`
ADD PRIMARY KEY (`AutID`);
ALTER TABLE `Books`
ADD PRIMARY KEY (`BookID`), ADD KEY `AutID` (`AutID`);
ALTER TABLE `Authors`
MODIFY `AutID` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
ALTER TABLE `Books`
MODIFY `BookID` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
ALTER TABLE `Books`
ADD CONSTRAINT `fkautid` FOREIGN KEY (`AutID`) REFERENCES `Authors` (`AutID`) ON DELETE NO ACTION ON UPDATE NO ACTION;and the following classes:
```
@Entity
@Table(name="Books"
,catalog="Autor"
)
public class Books implements java.io.Serializable {
private Integer bookId;
private Authors authors;
private String name;
public Books() {
}
public Books(Authors authors, String name) {
this.authors = authors;
this.name = name;
}
@Id @GeneratedValue(strategy=IDENTITY)
@Column(name="BookID", unique=true, nullable=false)
public Integer getBookId() {
return this.bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
@ManyToOne
@JoinColumn(name="AutID", nullable=false,insertable = false, updatable = false)
public Authors getAuthors() {
return this.authors;
}
public void setAuthors(Authors authors) {
this.authors = authors;
}
@Column(name="Name", nullable=false, length=50)
public String getName() {
return this.name;
}
public void setName(String name) {
Solution
Always use singular names for entities. It's
Always get the ID. Get the surrogate key, not the business key, from the UI.
You can use
Book, not Books; and Author, not Authors.Always get the ID. Get the surrogate key, not the business key, from the UI.
You can use
Session.load to set up foreign key relations without querying the DB.void addBook(int authorId, String bookName) {
Session session = //...get a session
Author author = session.load(Author.class, authorId);
Book book = new Book(author, bookName);
s.save(b);
}Code Snippets
void addBook(int authorId, String bookName) {
Session session = //...get a session
Author author = session.load(Author.class, authorId);
Book book = new Book(author, bookName);
s.save(b);
}Context
StackExchange Code Review Q#86872, answer score: 4
Revisions (0)
No revisions yet.