I have user entity in backend and another is feedback entity userid of user table is foreign key to feedback table. I want to print that user id in frontend part. But when I submit any feedback in database it is adding the userid correctly. IN frontend I have created table to display all feedbacks I want to print userid in that table it is coming blank.
This is my feedback entity:
#Entity
#Table(name="feedback")
public class Feedback implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "FEEDBACK_ID")
private int feedbackid;
#Column(name = "FEEDBACK_DATE")
private LocalDate feedbackDate;
#Column(name = "TITLE")
private String title;
#Column(name = "DESCRIPTION")
private String description;
#Column(name = "STATUS")
private String status;
#JsonIgnore
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "USER_ID")
#JsonIncludeProperties({"userId"})
#JsonIgnoreProperties({"userName", "firstName", "lastName",
"dateOfBirth", "email", "mobileno", "password",
"subscriptionStartDate", "subscriptionExpireDate", "subscriptionStatus"})
private User user;
How should I write the Typescript to display user id also
Related
I have the REST API app and when I create a user with the same username it creates all the time, but I want the username to be unique cause in real apps username is unique for each user, how can I provide a checking username in DB?
I want my app to throw some message when I create a user with a username that already exists, and of course, do not save a new user username that already exists
My user Entity:
#Table(name = "usr", uniqueConstraints=
#UniqueConstraint(columnNames={"username", "email"}))
public class User {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "username")
private String username;
#Column(name = "password")
private String password;
#Column(name = "first_name")
private String firstName;
#Column(name = "last_name")
private String lastName;
#Column(name = "role")
private Role role;
#Column(name = "email")
private String email;
#Column(name = "status")
private Status status;
// #OneToMany(mappedBy = "author")
// private List<Message> messages;
// #OneToMany(mappedBy = "author")
// private List<Comment> comments;
#OneToOne
#JoinColumn(name = "gender_id")
private Gender gender;
#OneToOne
#JoinColumn(name = "address_id")
private Address address;
#ManyToMany
#JoinTable(
name = "user_subscriptions",
joinColumns = #JoinColumn(name = "channel_id") ,
inverseJoinColumns = #JoinColumn(name = "subscriber_id"))
private Set<User> subscribers;
#ManyToMany
#JoinTable(
name = "user_subscriptions",
joinColumns = #JoinColumn(name = "subscriber_id") ,
inverseJoinColumns = #JoinColumn(name = "channel_id"))
private Set<User> subscriptions;
#OneToOne
#JoinColumn(name = "file_id")
private FileEntity fileEntity;
My user service:
#Transactional
public User save(UserRequest user) {
provideAllUserCheckingActionsForSave(user);
User userUntilSave = userMap.userRequestToUser(user,Role.USER,Status.ACTIVE);
userUntilSave.setPassword(passwordEncoder.encode(userUntilSave.getPassword()));
User saved = userRepo.save(userUntilSave);
log.info("User save method invoked");
return saved;
}
You can use #unique annotation to store the unique values in the database and if it throws the exception for not taking the unique value the exception should be handled.
Before create user,you can search user by username,if search result is empty,then create new user,if search result is not empty,deal the question.
You can add validation to the controller or you can simply do it as follows:- When a user wants to register, get the username and search in the database whether it already exists or not. If findByUsername(username) returns null then perform the next operation and register the user, else return some error message.
I have to entities
#Table(name = "BD_PERSON")
public class DBPerson {
#Id
#Column(name = "persion_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "name")
private String name;
#Column(name = "car_id")
private Long car_id;
#OneToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "car_id", referencedColumnName = "car_id", insertable = false, updatable = false)
private DBCar car;
#Column(name = "label")
private String label;
and
#Table(name = "BD_CAR")
public class DBCar {
#Id
#Column(name = "car_id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name = "car_number")
private Integer carNumber;
#Column(name = "description")
private String description;
I want to be able to get the Person with the car entity but this should not persist to the database when updating.
But i should be able to update the car_id by itself as it relates the the id in the car table.
but when i try to test this by setting only the car_id i receive an Referential integrity constraint violation.
Because the car_id (FK)references the car_id (PK)
Is there any better way to accomplish what im trying to do?
The database already exists and is configured like this where the car_id is not an actual foreign key
I have 4 tables here: Register, User, User roles and User Profile Image
Register and User are mapped my a One to One relationship and a reference of Register is generated in Users table...... This is fine..
Now talking about One to Many Relation between User and the Roles table, it also works perfectly by generating a User table reference in the roles table..
But problem is when working with One to One between User and the Profile Image. Profile Image is not generating reference of User....Why the user reference is not generated in Profile Image table
Register
#Entity
#Getter
#Setter
public class Register {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#OneToOne(cascade = CascadeType.ALL,orphanRemoval = true,mappedBy = "register")
private User user;
}
User
#Entity
#Getter
#Setter
public class User {
#Id
#Column(name = "User_Id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
#JoinColumn(name = "user_id")
private UserProfileImage userProfileImage;
#OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
#JoinColumn(name = "user_id")
private List<UserRoles> userRoles;
}
User Profile Image
#Entity
#Getter
#Setter
public class UserProfileImage {
#Id
#Column(name = "id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "name")
private String name;
#Column(name = "type")
private String type;
#Column(name = "picByte", length = 100000)
private byte[] picByte;
public UserProfileImage() {
super();
}
public UserProfileImage(String name, String type, byte[] picByte) {
this.name = name;
this.type = type;
this.picByte = picByte;
}
}
Profile mapping in User class is not correct and in your profile class there is no user field and hence it's not generating the user reference in the profile class.
Also, User to Roles mapping is also not correct, your user class will not populate roles with your mappings.
Try this:
public class User {
...
#OneToOne(cascade = CascadeType.ALL,fetch=FetchType.EAGER)
#JoinColumn(name = "PROFILE_IMAGE_ID") // foreign key column in User table
private UserProfileImage userProfileImage;
#OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER,mappedBy="user")
private List<UserRoles> userRoles;
}
public class UserProfileImage {
...
#OneToOne(mappedBy="userProfileImage")
private User user;
...
}
public class UserRole {
...
#ManyToOne
#JoinColumn(name="USER_ID") // foreign key column in User Role table
private User user;
...
}
I have 2 entities User and Venue. One user can have multiple venues. The table looks like this:
user_id is the foreign key in the Venue table.
I am trying to create a query for retrieving all Venues by user_id. This is what I have:
#Override
public List<Venue> findVenueByOwnerId(Long userId) {
return em.createQuery("select v from Venue v where v.user.id = :ownerId", Venue.class)
.setParameter("ownerId", userId)
.getResultList();
}
What am I doing wrong?
EDIT:
These are my mappings:
For User class:
#Entity
public class User {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", updatable = false, nullable = false)
private Long id;
private String email;
private String username;
private String password;
private String firstName;
private String lastName;
#OneToMany
#JoinColumn(name = "user_id")
private List<Venue> venues;
For Venue class:
#Entity
public class Venue {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", updatable = false, nullable = false)
private Long id;
private String name;
private String location;
private int capacity;
#ManyToOne
#Column(name = "user_id", updatable = false, nullable = false)
private User user;
And if I am trying to add getters and setters for the User in the Venue class, it throws me this error:
Caused by: org.hibernate.MappingException: Could not determine type for: razvan.sem3.project.model.User, at table: Venue, for columns: [org.hibernate.mapping.Column(user)]
I want to do a query(SELECT) with inner join using JPQL.
I'm using hibernate ddl auto to create tables from the entities.
I have 2 entities, they're relacionated among them by many-to-many relationship(One studio can be managed by many managers(user) and one manager(user) can manage many studios).
As you know, for many-to-many relationship, we use an intermediate table to do a "SELECT" with native SQL, but i'm using JPA and JPQL as query lenguage, so my question is: How to do JOIN SELECT between 2 tables relacionated among them by many-to-many relationship?
The entities below are my entities:
Entity 1(Studio):
#Entity
#Table(name = "studio")
public class Studio implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
#Column(name = "studio_name", nullable = false)
private String studioName;
/**
* Indicates if the studio is active or inactive in the data base.
*/
#Column(name = "active")
private Boolean active;
/**
* The studio owner is the main manager for a studio.
*/
#ManyToOne(fetch = FetchType.EAGER)
#JoinColumn(name = "studio_owner", referencedColumnName = "id", nullable = false)
private User studioOwner;
/**
* Represents all the possible managers for this studio.
*/
#ManyToMany(fetch = FetchType.LAZY)
private List<User> studioManagers;
//--Getters and setter below
}
Entity 2(User):
#Entity
#Table(name = "userr")
public class User implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
#Column(name = "user_name", nullable = false, unique = true)
private String userName;
#Column(name = "email", nullable = false, unique = true)
private String email;
#Column(name = "password", length = 60)
private String password;
#Column(name = "name")
private String name;
#Column(name = "last_name")
private String lastName;
#Column(name = "user_state", nullable = false)
#Enumerated(EnumType.STRING)
private UserState userState;
/**
* The roles for this user.
*/
#ManyToMany(fetch = FetchType.EAGER)
private List<Role> roles;
public User() {
}
//--Getters and setter below
}
I'm sorry by me English, Im Spanish speaker.
In StudioRepository.java just write one method
List<Studio> findById(Integer id);
this will fetch studio with all the users associated with this studio.
eg:
Studio{
"id": 1,
"studioName": "some",
"studioOwner": [
{
.....
....
},
{
....
}
]
}