Mapping one of two classes that implements interface - java

Class has property that can be one of two class-types, that's why I try to create interface that class-types implement
#Entity
#Table(name = "users")
public class User{
#Id
#Column(name="id")
#GeneratedValue(strategy=GenerationType.AUTO)
private Long userID;
#Column(name="email")
private String email;
#OneToOne(mappedBy = "user")
private Login login;
... getters/setters
#MappedSuperclass
public interface Login {
User user = new User();
}
#Entity
#Table(name = "user_logins_social")
#IdClass(UserLoginSocialID.class)
public class UserLoginSocial implements Login{
#OneToOne
#JoinColumn(name="uid")
private User user;
...
#Entity
#Table(name = "user_logins_native")
public class UserLoginNative implements Login{
#OneToOne
#JoinColumn(name="uid")
private User user;
...
So I can't use target entity because both classes can be used. Here is error stack: org.hibernate.AnnotationException: Unknown mappedBy in: model.User.login, referenced property unknown: model.Login.user. Help me please

Cannot map or query on an inferface.
#MappedSuperclass should be able to use abstract class and class.
Please check: wikihttp://en.wikibooks.org/wiki/Java_Persistence/Advanced_Topics#Interfaces
#MappedSuperclass
public abstract class User{
#Id
#Column(name="id")
#GeneratedValue(strategy=GenerationType.AUTO)
private Long userID;
#Column(name="email")
private String email;
#OneToOne(mappedBy = "user")
private Login login;
// ... getters/setters
#Entity
#Table(name = "login")
public class Login{
#OneToOne
private User user;
}
#Entity
#Table(name = "user_logins_social")
#IdClass(UserLoginSocialID.class)
public class UserLoginSocial extends User{
//...
}
#Entity
#Table(name = "user_logins_native")
public class UserLoginNative extends User{
//...
}

Related

Java Repository Query findAll() given an ID from another class (Foreign-Key)

I'm using a JPARepository called PublicationRepository and want to find all Publications from a certain Person. This Classes are connected over the Class Author.
Person Class:
#Entity
public class Person {
#Id
private String email;
private String telefon;
private String password;
#OneToMany(mappedBy = "person")
Set<Author> Author;
}
Author Class:
#Entity
#Data
#NoArgsConstructor
#AllArgsConstructor
public class Author {
#Id
private int id;
#ManyToOne
#JoinColumn(name="Person_ID")
Person person;
#ManyToOne
#JoinColumn(name="Publication_ID")
Publication publication;
private String Date;
private String Writerstatus;
}
Publication Class
#Entity
#AllArgsConstructor
#NoArgsConstructor
#Data
public class Publication {
#Id
private int id;
private String publicationname;
#OneToMany(mappedBy = "publication")
Set<Author> author;
}
And the PublicationRepository
public interface ProjektRepository extends JpaRepository<Projekt,Integer> {
}
public interface PublicationRepository extends JpaRepository<Publication,Integer> {
#Query(value = "SELECT pub.* FROM author as auth INNER JOIN publications as pub ON auth.publication_id = pub.id WHERE auth.person_id = ?1", native = true)
List<Publication> findAllPublicationsOfThisPerson(int personId);
}
Try this.
I would also recommend to annotate the entities with their table names:
#Table(name = "publication")
You use a manually build table for a Many-to-Many relationship Author
You could also delegate that to Spring Data Jpa by using #ManyToMany Annotation.
A good tutorial:
https://attacomsian.com/blog/spring-data-jpa-many-to-many-mapping

is there a #MappedSuperclass equivalent annotation for mongoDB in spring?

In my project, we are moving from SQL to NoSQL to a certain extent.
I wanted to know, how can we inherit BaseClass properties into child classes in spring data mongo.
I know how to do it in Spring JPA for SQL.
Example,
Below is BaseEntity parent class which is annotated with #MappedSuperClass
It has id and version as its fields.
#MappedSuperclass
public class BaseEntity {
#Id
#GeneratedValue
private Long id;
#Version
private Integer version;
//Getters and setters omitted for brevity
}
entities can extend the BaseEntity class and skip declaring the #Id or #Version properties since they are inherited from the base class.
#Entity(name = "Post")
#Table(name = "post")
public class Post extends BaseEntity {
private String title;
#OneToMany
private List comments = new ArrayList();
#OneToOne
private PostDetails details;
#ManyToMany
#JoinTable(//Some join table)
private Set tags = new HashSet();
//Getters and setters omitted for brevity
public void addComment(PostComment comment) {
comments.add(comment);
comment.setPost(this);
}
public void addDetails(PostDetails details) {
this.details = details;
details.setPost(this);
}
public void removeDetails() {
this.details.setPost(null);
this.details = null;
}
}
#Entity(name = "PostComment")
#Table(name = "post_comment")
public class PostComment extends BaseEntity {
#ManyToOne(fetch = FetchType.LAZY)
private Post post;
private String review;
//Getters and setters omitted for brevity
}
How can I implement same thing in Mongo? For example
#MappedSuperclass
public class BaseEntity {
#Id
#GeneratedValue
private Long id;
#Version
private Integer version;
//Getters and setters omitted for brevity
}
#Document(collection = "Post")
public class Post extends BaseEntity {
private String title;
//Rest of the code
}
#Document(collection = "PostComment")
public class PostComment extends BaseEntity {
#ManyToOne(fetch = FetchType.LAZY)
private Post post;
private String review;
//Getters and setters omitted for brevity
}
You do not need any annotation to do that in Mongo. Mongo itself will take care of superclass for you.
Just extend BaseEntity class in all your entities, all entities will have fields from BaseEntity class when you read and write entities to database. This also works at multilevel hierarchy. i.e. Post extends BaseEntity, BaseEntity extends Entity, in this case Post will have fields from both BaseEntity and Entity class.

Can I map multiple database tables to a unique Entity using JPA?

I am trying to add multi-language to the description and title fields of one of my Entities but without success. My Entity is like below and my database is MySQL:
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "project")
public class ProjectEntity implements Serializable {
#Id
#Column(name="id")
private Integer id;
#Column(name="team_size")
private Integer teamSize;
#Column(name="description")
private String description;
#Column(name="title")
private String title;
#OneToMany
private List<DetailsEntity> details;
}
I alreaady tried to add a projectDetails entity that contains the description and title, but as I need multilanguage, the Project will now have a list of ProjectDetails in my backend, what I don't need.
Here is what the projectDetails could looks like:
#Data
#AllArgsConstructor
#NoArgsConstructor
#Entity
#Table(name = "project_details")
public class ProjectDetails implements Serializable {
#Id
#Column(name="id")
private Integer id;
#Column(name="title")
private String title;
#Column(name="description")
private String description;
#Column(name="language")
private String language;
#Column(name="project_id")
private Integer projectId;
}
I would like to be able to do something with the JPA repository requests, like this:
#Repository
public interface ProjectEntityRepository extends JpaRepository<ProjectEntity, Integer> {
#Query("select p from ProjectEntity p left outer join ProjectDetails d on p.id=d.project_id and d.language=:language")
List<ProjectEntity> findAllForLanguage(String language);
}
Any idea on how to change part of this to return the Project entity with values of only 1 ProjectDetails?
Thank you

SEC1111: Cannot load group for JDBC realm user

I apparently doing something wrong in the configuration of a jdbcRealm. But I don't know what exactly.
This is my configuration:
What else is needed to help me?
#Entity
#Table(name = "gebruiker")
public class User implements Serializable {
#Id #Column(name="User_NAME")
private String name;
#ManyToOne
#JoinTable(name = "groep_gebruiker")
private Role rol;
#Entity
#Table(name = "groep")
public class Role implements Serializable {
#Id
private String rol;
#OneToMany(mappedBy = "rol")
private Collection<User> gebruikers = new ArrayList();
Obviously this is because you did not create a JDBC realm user /Hans/

Ebean: #OneToMany to the same Entity

I have the following scenario where a User can make a Reservation on a Campaign.
Eg. User wants to reserve a Campaign(eg 20% off on a book). I want the reservation table to look like this: |id|user_id|campaign_id|
User can make many reservations.
Campaign can be reserved by many users.
Reservation will contain what user reserved what campaign.
User.java
#Entity
public class User extends Model {
#Id
public Long id;
public String username;
public String password;
#OneToMany(cascade = CascadeType.PERSIST)
public List<Reservation> reservations;
}
Campaign.java
#Entity
public class Campaign extends Model {
#Id
public Long id;
public String name;
#OneToMany(cascade = CascadeType.PERSIST)
public List<Reservation> reservations;
}
Reservations.java
#Entity
public class Reservation extends Model {
#Id
public Long id;
}
How come this doesn't work?
EDIT:
I get a reservation table that has an id and an user_id, but no campaign_id
You may consider doing it like this,
User.java
#Entity
public class User extends Model {
#Id
public Long id;
public String username;
public String password;
#OneToMany(cascade = CascadeType.PERSIST,mappedBy="user")
public List<Reservation> reservations;
}
Campaign.java
#Entity
public class Campaign extends Model {
#Id
public Long id;
public String name;
#OneToMany(cascade = CascadeType.PERSIST,mappedBy="campaign")
public List<Reservation> reservations;
}
Reservation.java
#Entity
public class Reservation extends Model {
#Id
public Long id;
#ManyToOne
public User user;
#ManyToOne
public Campaign campaign;
}

Categories