Skip to content

Commit 1a067bc

Browse files
lor6KevinGilmore
authored andcommitted
models, repos, test (eugenp#1225)
* models, repos, test * update models and test * updated test location * update test location
1 parent 7f20437 commit 1a067bc

9 files changed

Lines changed: 394 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.models;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.Id;
7+
import javax.persistence.OneToOne;
8+
9+
@Entity
10+
public class Address {
11+
12+
@Id
13+
@GeneratedValue
14+
private long id;
15+
16+
@Column(nullable = false)
17+
private String location;
18+
19+
@OneToOne(mappedBy = "address")
20+
private Library library;
21+
22+
public Address() {
23+
}
24+
25+
public Address(String location) {
26+
super();
27+
this.location = location;
28+
}
29+
30+
public long getId() {
31+
return id;
32+
}
33+
34+
public void setId(long id) {
35+
this.id = id;
36+
}
37+
38+
public String getLocation() {
39+
return location;
40+
}
41+
42+
public void setLocation(String location) {
43+
this.location = location;
44+
}
45+
46+
public Library getLibrary() {
47+
return library;
48+
}
49+
50+
public void setLibrary(Library library) {
51+
this.library = library;
52+
}
53+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.models;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.CascadeType;
6+
import javax.persistence.Column;
7+
import javax.persistence.Entity;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.Id;
10+
import javax.persistence.JoinColumn;
11+
import javax.persistence.JoinTable;
12+
import javax.persistence.ManyToMany;
13+
14+
@Entity
15+
public class Author {
16+
17+
@Id
18+
@GeneratedValue
19+
private long id;
20+
21+
@Column(nullable = false)
22+
private String name;
23+
24+
@ManyToMany(cascade = CascadeType.ALL)
25+
@JoinTable(name = "book_author", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id"))
26+
private List<Book> books;
27+
28+
public Author() {
29+
}
30+
31+
public Author(String name) {
32+
super();
33+
this.name = name;
34+
}
35+
36+
public String getName() {
37+
return name;
38+
}
39+
40+
public void setName(String name) {
41+
this.name = name;
42+
}
43+
44+
public long getId() {
45+
return id;
46+
}
47+
48+
public void setId(long id) {
49+
this.id = id;
50+
}
51+
52+
public List<Book> getBooks() {
53+
return books;
54+
}
55+
56+
public void setBooks(List<Book> books) {
57+
this.books = books;
58+
}
59+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.models;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
import javax.persistence.JoinColumn;
10+
import javax.persistence.ManyToMany;
11+
import javax.persistence.ManyToOne;
12+
13+
@Entity
14+
public class Book {
15+
16+
@Id
17+
@GeneratedValue
18+
private long id;
19+
20+
@Column(nullable = false)
21+
private String title;
22+
23+
@ManyToOne
24+
@JoinColumn(name = "library_id")
25+
private Library library;
26+
27+
@ManyToMany(mappedBy = "books")
28+
private List<Author> authors;
29+
30+
public Book() {
31+
}
32+
33+
public Book(String title) {
34+
super();
35+
this.title = title;
36+
}
37+
38+
public String getTitle() {
39+
return title;
40+
}
41+
42+
public void setTitle(String title) {
43+
this.title = title;
44+
}
45+
46+
public long getId() {
47+
return id;
48+
}
49+
50+
public void setId(long id) {
51+
this.id = id;
52+
}
53+
54+
public Library getLibrary() {
55+
return library;
56+
}
57+
58+
public void setLibrary(Library library) {
59+
this.library = library;
60+
}
61+
62+
public List<Author> getAuthors() {
63+
return authors;
64+
}
65+
66+
public void setAuthors(List<Author> authors) {
67+
this.authors = authors;
68+
}
69+
70+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.models;
2+
3+
import java.util.List;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.GeneratedValue;
8+
import javax.persistence.Id;
9+
import javax.persistence.JoinColumn;
10+
import javax.persistence.OneToMany;
11+
import javax.persistence.OneToOne;
12+
13+
import org.springframework.data.rest.core.annotation.RestResource;
14+
15+
@Entity
16+
public class Library {
17+
18+
@Id
19+
@GeneratedValue
20+
private long id;
21+
22+
@Column
23+
private String name;
24+
25+
@OneToOne
26+
@JoinColumn(name = "address_id")
27+
@RestResource(path = "libraryAddress")
28+
private Address address;
29+
30+
@OneToMany(mappedBy = "library")
31+
private List<Book> books;
32+
33+
public Library() {
34+
}
35+
36+
public Library(String name) {
37+
super();
38+
this.name = name;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
public long getId() {
50+
return id;
51+
}
52+
53+
public void setId(long id) {
54+
this.id = id;
55+
}
56+
57+
public Address getAddress() {
58+
return address;
59+
}
60+
61+
public void setAddress(Address address) {
62+
this.address = address;
63+
}
64+
65+
public List<Book> getBooks() {
66+
return books;
67+
}
68+
69+
public void setBooks(List<Book> books) {
70+
this.books = books;
71+
}
72+
73+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.repositories;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
import com.baeldung.models.Address;
6+
7+
public interface AddressRepository extends CrudRepository<Address, Long> {
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.repositories;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
import com.baeldung.models.Author;
6+
7+
public interface AuthorRepository extends CrudRepository<Author, Long> {
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.repositories;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
import com.baeldung.models.Book;
6+
7+
public interface BookRepository extends CrudRepository<Book, Long> {
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.baeldung.repositories;
2+
3+
import org.springframework.data.repository.CrudRepository;
4+
5+
import com.baeldung.models.Library;
6+
7+
public interface LibraryRepository extends CrudRepository<Library, Long> {
8+
9+
}

0 commit comments

Comments
 (0)