I'm having this error
Error accessing field [private java.lang.Integer
fearx.projects.animal.finder.api.entity.LoginEntity.id] by reflection
for persistent property
[fearx.projects.animal.finder.api.entity.LoginEntity#id] : 2; nested
exception is
org.hibernate.property.access.spi.PropertyAccessException: Error
accessing field [private java.lang.Integer
fearx.projects.animal.finder.api.entity.LoginEntity.id] by reflection
for persistent property
[fearx.projects.animal.finder.api.entity.LoginEntity#id] : 2
While I try to do this:
public void registerUser(OwnerEntity body) {
ownerRepository.save(body);
loginRepository.save(new LoginEntity(body.getEmail(), body.getPassword()));
LoginEntity loginEntity = loginRepository.findByEmail(body.getEmail());
OwnerEntity ownerEntity = ownerRepository.findByEmail(body.getEmail());
ownerLoginRepository.save(new OwnerLoginEntity(loginEntity.getId(),ownerEntity.getId()));
}
My entitys:
#Entity(name = "owner_login")
#Data
public class OwnerLoginEntity {
#Id
#GeneratedValue
private Integer id;
#ManyToOne(targetEntity = LoginEntity.class)
#JoinColumn(name = "id_login", referencedColumnName = "id")
private Integer login;
#ManyToOne(targetEntity = OwnerEntity.class)
#JoinColumn(name = "id_owner", referencedColumnName = "id")
private Integer owner;
public OwnerLoginEntity(Integer login, Integer owner) {
this.login = login;
this.owner = owner;
}
}
public class OwnerEntity {
#Id
#GeneratedValue
private Integer id;
#Column
private String name;
#Column
private String email;
#Column
private String phone;
#Column
private String password;
#ManyToOne(targetEntity = LostPetEntity.class)
#JoinColumn(name = "id_pet", referencedColumnName = "id")
private Integer id_pet;
}
#Entity(name = "login")
#Data
public class LoginEntity {
#Id
#GeneratedValue
private Integer id;
#Column(unique = true, nullable = false)
private String email;
#Column
private String password;
public LoginEntity(String email, String password) {
this.email = email;
this.password = password;
}
public LoginEntity() {
}
}
O don't know what database you have, but try to change the approach of GeneratedValue like this:
#Entity(name = "owner_login")
#Data
public class OwnerLoginEntity {
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
#ManyToOne
private LoginEntity login;
#ManyToOne
private OwnerEntity owner;
public OwnerLoginEntity(LoginEntity login, OwnerEntity owner) {
this.login = login;
this.owner = owner;
}
}
Then you can call ownerLoginRepository.save(new OwnerLoginEntity(loginEntity, ownerEntity));
Related
I want to return a Profile Object in JSON containing a list of login details associated with a social network.
Everything works correctly when the "reseaux_sociaux" table is empty. For my status table I get my statuses in JSON format in my Profile object. However, when "reseaux_sociaux" contains values then I get the error below and my Profile object in JSON format is not returned...
(Logs)
https://cloudvyzor.com/logpad/?query&database=sandbox-7fb06b2c06f198a7c0e4ff7c74d659e0
Profil Class
#Entity
#Table(name = "Profil")
public class Profil {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long Id;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "IdComptes", nullable = false)
private Comptes IdComptes;
private String Avatar;
private String Banniere;
private String Pseudo;
private String MailPro;
private String Bio;
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "Statut_Profil", joinColumns = #JoinColumn(referencedColumnName = "Id"),inverseJoinColumns = #JoinColumn(referencedColumnName ="Id"))
private List<Statut> Statut;
#OneToMany( mappedBy = "IdProfil")
#JsonManagedReference("id_profil")
private List<ReseauxSociaux> Reseaux;
public Profil(){}
public Profil(Long id, Comptes idComptes, String avatar, String banniere, String pseudo, String mailPro, String bio) {
Id = id;
IdComptes = idComptes;
Avatar = avatar;
Banniere = banniere;
Pseudo = pseudo;
MailPro = mailPro;
Bio = bio;
}
}
ReseauxSociaux Class
#Entity
#IdClass(ReseauxId.class)
public class ReseauxSociaux {
#Id
private int Id;
#Id
private Long IdProfil;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "IdProfil", insertable = false, updatable = false)
#JsonBackReference("id_profil")
private Profil Profil;
#ManyToOne
#JoinColumn(name = "Id", insertable = false, updatable = false)
#JsonBackReference("id")
private Reseau Reseau;
private String Identifiant;
private ReseauxSociaux()
{}
public ReseauxSociaux(int id, Long idProfil, String identifiant) {
Id = id;
IdProfil = idProfil;
Identifiant = identifiant;
}
}
Reseau class
#Entity
public class Reseau {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int Id;
private String Nom;
private String Couleur;
//I tried it with and without and it made no difference
#OneToMany( mappedBy = "Id")
#JsonManagedReference("id")
private List<ReseauxSociaux> Reseaux;
public Reseau(){}
public Reseau(int id, String nom, String couleur) {
Id = id;
Nom = nom;
Couleur = couleur;
}
//Get Set
}
Profil Controller
#RestController
#RequestMapping("/profil")
public class ProfilController {
private final ProfilRepository profilRepository;
public ProfilController(ProfilRepository profilRepository) {
this.profilRepository = profilRepository;
}
#PostMapping("/getprofil/{idCompte}")
Profil GetProfil(#PathVariable("idCompte") Long idCompte)
{
Profil profil= profilRepository.findProfilById(idCompte);
return profil;
}
}
I finally succeeded... The cause of the problem remains unclear but I have two hypotheses: the first is the use of capital letters on variable names; the second is the use of a list with the onetomany with the same name in two entities.
Profil class
#Entity
#Table(name = "Profil")
public class Profil {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#ManyToOne(fetch = FetchType.LAZY, optional = false)
#JoinColumn(name = "idComptes", nullable = false)
#JsonBackReference("id_comptes")
private Comptes idComptes;
private String avatar;
private String banniere;
private String pseudo;
private String mailPro;
private String bio;
#ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinTable(name = "statut_profil", joinColumns = #JoinColumn(referencedColumnName = "id"),inverseJoinColumns = #JoinColumn(referencedColumnName ="id"))
private List<Statut> statut;
#OneToMany( mappedBy = "idProfil")
#JsonManagedReference("id_profil")
private List<ReseauxSociaux> lstprofil;
public Profil(){}
public Profil(Long id, Comptes idComptes, String avatar, String banniere, String pseudo, String mailPro, String bio) {
this.id = id;
this.idComptes = idComptes;
this.avatar = avatar;
this.banniere = banniere;
this.pseudo = pseudo;
this.mailPro = mailPro;
this.bio = bio;
}
//get set
}
ReseauxSociaux class
#Entity
#IdClass(ReseauxId.class)
public class ReseauxSociaux {
#Id
private int id;
#ManyToOne
#JoinColumn(name = "id", insertable = false, updatable = false)
#JsonBackReference("id_reseau")
private Reseau reseau;
#Id
private Long idProfil;
#ManyToOne
#JoinColumn(name = "idProfil", insertable = false, updatable = false)
#JsonBackReference("id_profil")
private Profil profil;
private String identifiant;
private ReseauxSociaux()
{}
public ReseauxSociaux(int id, Reseau reseau, Long idProfil, String identifiant) {
this.id = id;
this.reseau = reseau;
this.idProfil = idProfil;
this.identifiant = identifiant;
}
}
Reseau class
#Entity
public class Reseau {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String nom;
private String couleur;
#OneToMany( mappedBy = "id")
#JsonManagedReference("id_reseau")
private List<ReseauxSociaux> lstreseau;
public Reseau(){}
public Reseau(int id, String nom, String couleur) {
this.id = id;
this.nom = nom;
this.couleur = couleur;
}
}
I created objects User and Message, and now I want, that message's id will be associated with the user id. How can I do it with annotations or with something else?
Table User
Table Messge
Message class
#Entity
public class Message {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
private String tag;
}
User class
#Entity
#Table(name = "usr")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private boolean active;
#ElementCollection(targetClass = Role.class , fetch = FetchType.LAZY)
#CollectionTable(name = "user_role" , joinColumns = #JoinColumn(name = "user_id"))
#Enumerated(EnumType.STRING)
private Set<Role> roles;
}
you can try this:
Message class:
#Entity
public class Message {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String text;
private String tag;
#ManyToOne
#JoinColumn(name = "user_id")
private User userId;
}
user class:
#Entity
#Table(name = "usr")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String username;
private String password;
private boolean active;
#ElementCollection(targetClass = Role.class , fetch = FetchType.LAZY)
#CollectionTable(name = "user_role" , joinColumns = #JoinColumn(name = "user_id"))
#Enumerated(EnumType.STRING)
private Set<Role> roles;
#OneToMany(mappedBy = "user")
#JsonBackReference
private set<Message> messages;
}
I'm trying to use a foreign key in Hibernate/Spring Boot with the #OneToMany and #ManyToOne annotations, but I keep getting this error:
java.sql.SQLException: Field 'note_id' doesn't have a default value
My code:
Note.java
#Entity
#Table(name = "notes")
#EntityListeners(AuditingEntityListener.class)
public class Note {
public Note() {
}
public Note(String name, String content) {
this.name = name;
this.content = content;
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "note_id")
private Integer id;
#ManyToOne
#JoinColumn(name = "user_id")
private User user;
#NotBlank
private String name;
#NotBlank
#Column(columnDefinition="LONGTEXT")
private String content;
User.java
#Entity
#Table(name = "user")
#EntityListeners(AuditingEntityListener.class)
public class User implements Serializable {
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "user_id")
private Integer id;
#OneToMany(mappedBy = "user", cascade = ALL)
private Set<Note> notes;
#NotEmpty
#Column(nullable = false, unique = true)
private String username;
#NotEmpty
private String password;
The error occurs whenever I try to create a note.
Thanks in advance!
I have been using many to many mapping here is my POJO classes.
Menu.java :
#Entity
#Table(name = "menu")
public class Menu {
#Id
#Column(name = "menuid")
#GeneratedValue
private int menuid;
#Column(name = "parentid")
private int parentid;
#Column(name = "menuname")
private String menuname;
#Column(name = "url")
private String url;
#Column(name = "status")
private String status;
#Column(name = "usertype")
private String usertype;
#Column(name = "isparent")
private boolean isParent;
private ArrayList<Menu> childMenu;
#ManyToMany(mappedBy="menus")
private List<User> users;
public Menu(Integer menuid){
this.menuid=menuid;
}
public Menu(){
}
User.java :
#Entity
#Table(name = "user")
public class User implements Serializable {
#Id
#Column(name = "userid")
#GeneratedValue
private Integer userId;
#Column(name = "OUTLET_ID")
private int outletId;
#Column(name = "NAME")
private String name;
#Column(name = "USERTYPE")
private String userType;
#Column(name = "LOGINID")
private String loginId;
#Column(name = "PASSWORD")
private String password;
#Column(name = "CREATEDDATE")
private String createdDate;
#Column(name = "CONTACTNUMBER")
private String contactNumber;
#Column(name = "EMAILID")
private String emailId;
#Column(name = "OUTLETTYPE")
private String outlettype;
#Transient
private String nsec;
#javax.persistence.Transient
ArrayList<Integer> menuid;
#javax.persistence.Transient
ArrayList<Long> clientid;
#javax.persistence.Transient
ArrayList<String> clientName;
#ManyToMany(fetch=FetchType.EAGER) #JsonIgnore
#JoinTable(name="user_menu",joinColumns={#JoinColumn(name="userid")},
inverseJoinColumns={#JoinColumn(name="menuid")})
public List<Menu> menus;
#ManyToMany(fetch=FetchType.EAGER) #JsonIgnore
#JoinTable(name="user_client",joinColumns={#JoinColumn(name="userid")},
inverseJoinColumns={#JoinColumn(name="outletid")})
public List<Client> clients;
public User() {
}
I have user,menu and third mapping table user_menu which is created automatically, I successfully get result when fire following query in mysql
select * from menu m inner join user_menu um on m.menuid = um.menuid where um.userid = 41;
I want to write this query in hibernate how to this stuff ???
Finally I find my answer, here is my hql query,
String sql = "select m.menuid as menuid,m.parentid as parentid,m.menuname as menuname,m.url as url,m.status as status,m.usertype as usertype,m.isParent as isParent,m.childMenu as childMenu from Menu m join m.users u where u.userId = "+userid +"";
q = session.createQuery(sql).setResultTransformer(Transformers.aliasToBean(Menu.class));
I'm making an application that use a Rest Api to handle the communication between front and database.
Spring is used for the Java Api and Postgres as database.
The point is that I need a ternary relationship between three table. I explain myself, I have three table :
- user
- role
- project
Every user have a specific role depending on the project.
When I select a Project (in front), I need to display all the participants with their respective project.
Those are my classes :
#Entity
#Table(name = "users")
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
private String email;
#NotNull
#Column(name = "last_name")
private String lastName;
#NotNull
#Column(name = "first_name")
private String firstName;
#NotNull
private String password;
private String image;
#Entity
#Table(name = "projects")
public class Project {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#NotNull
private String name;
#NotNull
private Long version;
#NotNull
private Date date;
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
#JoinTable(name = "user_profile_project", joinColumns = #JoinColumn(name = "project_id", referencedColumnName = "id"), inverseJoinColumns = #JoinColumn(name = "user_id", referencedColumnName = "id"))
private List<User> participants;
#Entity
#Table(name = "profiles")
public class Profile {
private Long id;
private String name;
private Set<Permission> permissions = new HashSet<Permission>(0);
public Profile(){}
public Profile(String name) {
this.name = name;
}
public Profile(String name, Set<Permission> permissions) {
this.name = name;
this.permissions = permissions;
}
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", unique = true, nullable = false)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
#Column(name = "name", nullable = false)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}