Mongodb morphia (#7316)
* Adding source code for tutorial tracked by BAEL-2971 * Renaming Integration Test as par standard * Incorporated review comments on the article. * Moved the morphia module inside persistence-modules/java-mongodb * Deleted the module morphia.
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
9572af2804
commit
9566b4d5e4
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.morphia.domain;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
|
||||
@Entity
|
||||
public class Author {
|
||||
|
||||
@Id
|
||||
private String name;
|
||||
private List<String> books;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getBooks() {
|
||||
return books;
|
||||
}
|
||||
|
||||
public void setBooks(List<String> books) {
|
||||
this.books = books;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package com.baeldung.morphia.domain;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import dev.morphia.annotations.Embedded;
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Field;
|
||||
import dev.morphia.annotations.Id;
|
||||
import dev.morphia.annotations.Index;
|
||||
import dev.morphia.annotations.IndexOptions;
|
||||
import dev.morphia.annotations.Indexes;
|
||||
import dev.morphia.annotations.Property;
|
||||
import dev.morphia.annotations.Reference;
|
||||
import dev.morphia.annotations.Validation;
|
||||
|
||||
@Entity("Books")
|
||||
@Indexes({ @Index(fields = @Field("title"), options = @IndexOptions(name = "book_title")) })
|
||||
@Validation("{ price : { $gt : 0 } }")
|
||||
public class Book {
|
||||
@Id
|
||||
private String isbn;
|
||||
@Property
|
||||
private String title;
|
||||
private String author;
|
||||
@Embedded
|
||||
private Publisher publisher;
|
||||
@Property("price")
|
||||
private double cost;
|
||||
@Reference
|
||||
private Set<Book> companionBooks;
|
||||
|
||||
public Book() {
|
||||
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public double getCost() {
|
||||
return cost;
|
||||
}
|
||||
|
||||
public void addCompanionBooks(Book book) {
|
||||
if (companionBooks != null)
|
||||
this.companionBooks.add(book);
|
||||
}
|
||||
|
||||
public Book(String isbn, String title, String author, double cost, Publisher publisher) {
|
||||
this.isbn = isbn;
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
this.cost = cost;
|
||||
this.publisher = publisher;
|
||||
this.companionBooks = new HashSet<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [isbn=" + isbn + ", title=" + title + ", author=" + author + ", publisher=" + publisher + ", cost=" + cost + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((author == null) ? 0 : author.hashCode());
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(cost);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + ((isbn == null) ? 0 : isbn.hashCode());
|
||||
result = prime * result + ((publisher == null) ? 0 : publisher.hashCode());
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Book other = (Book) obj;
|
||||
if (author == null) {
|
||||
if (other.author != null)
|
||||
return false;
|
||||
} else if (!author.equals(other.author))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(cost) != Double.doubleToLongBits(other.cost))
|
||||
return false;
|
||||
if (isbn == null) {
|
||||
if (other.isbn != null)
|
||||
return false;
|
||||
} else if (!isbn.equals(other.isbn))
|
||||
return false;
|
||||
if (publisher == null) {
|
||||
if (other.publisher != null)
|
||||
return false;
|
||||
} else if (!publisher.equals(other.publisher))
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.morphia.domain;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
|
||||
import dev.morphia.annotations.Entity;
|
||||
import dev.morphia.annotations.Id;
|
||||
|
||||
@Entity
|
||||
public class Publisher {
|
||||
|
||||
@Id
|
||||
private ObjectId id;
|
||||
private String name;
|
||||
|
||||
public Publisher() {
|
||||
|
||||
}
|
||||
|
||||
public Publisher(ObjectId id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public ObjectId getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(ObjectId id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Catalog [id=" + id + ", name=" + name + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((name == null) ? 0 : name.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Publisher other = (Publisher) obj;
|
||||
if (name == null) {
|
||||
if (other.name != null)
|
||||
return false;
|
||||
} else if (!name.equals(other.name))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user