Problem with query custom using spring boot - mysql

I'm trying to make a custom query but the table name is not mapped. Thi is the code, can you help me please?
#Query("FROM sbootuserss WHERE age > 17")
This is the error:
org.hibernate.hql.internal.ast.QuerySyntaxException: sbootuserss is not mapped

Do you also have a model with the appropriate annotation in your application?
#Table(name = "sbootuserss")
public class Sbootuserss implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
// another fields ...
}
And the query:
#Query("FROM Sbootuserss WHERE age > 17")

Related

Failed to lazily initialize a collection of role , Spring

I'm trying to get info from an API in Spring, but it gives me an error because one of the fields is a Set. How to get a json with all of the info? If i use JsonIgnore, it won't give me the set i need, right?
My class:
#Data
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "saloon")
public class Saloon implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
#OneToMany(mappedBy = "saloon")
private Set<Service> services;
...
Collection valued attributes are by default lazily-fetched. One easy solutio is to changing it to
#OneToMany(mappedBy = "saloon", fetch = FetchType.EAGER)
But that might cause severe performance issues in various cases.
To get to an optimal solution, you need to analyze your design, ie how you are using this property in your code.
You can take a look at OpenEntityManagerInViewInterceptor.

How can I make hibernate not override ID in object to be saved, and rather use its ID?

I'm trying to save a Foo object with ID=20. In my MySQL db the last row has Foo with ID=5. When I use the save function in the JpaRepository, it saves the object, but instead of using the ID I wanted it to have (20), it uses 6.
#Entity
#Data
#ToString
#NoArgsConstructor
#JsonIgnoreProperties(ignoreUnknown = true)
public class Foo implements Serializable {
#Id
private long id;
I have tried with #GeneratedValue(strategy = GenerationType.IDENTITY), but that doesn't work either. Is there a way to configure Hibernate not take the next sequential ID, but use what is stored in the object?

Remove Duplicate entry '59' for key 'PRIMARY in Hibernate

I am very new in Hibernate. I am using Hibernate with JPA. I have an annotated entity class and a table related to that entity class.
#Entity
public class Test implements Serializable {
#Id
#GenericGenerator(name="inc" , strategy="identity")
#GeneratedValue(generator="inc")
private int id;
private String address; // setter getter and constructor
}
When saving this entity, it insert the data into the db. But during application running process another application is inserting data into same table. when my application try to save the data then Duplicate entry '59' for key 'PRIMARY' exception generated. So I want to use a generator which can insert the data and generate id in database level rather than application level and the identifier must saved back to my entity.
Use the Table generator strategy or the sequence generator.
You do not have to specify a generator. You can use the default generator and never set the id manually. If the error still comes post your merge/persist method.
More informations about generators can you found here https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing
#Entity
public class Test implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String address; // setter getter and constructor
}

mappedBy issue with Ebean/Play framework

I try to create a link between two tables : User and Notifications
The Notifications tables should have:
Id (int)
User_Id (int)
List < User >
I need to clarify my goal. In my app, a user ask something to several others, using notifications tables. So that, we can know:
Who is the user who asks the question (User_Id)
To whow users the question is aked (List< User >)
User
#Entity
public class User extends Model{
#ManyToOne
#JoinColumn(name="notification_fk")
public Notifications notification;
}
Notifications
#Entity
public class Notifications extends Model{
#Id
#GeneratedValue
public int id;
public User user;
#OneToMany(cascade=CascadeType.ALL, mappedBy="notifications")
public List<User> asked_users = new ArrayList<User>();
}
But I get the following error:
javax.persistence.PersistenceException: Error on
models.Notifications.asked_users Can not find mappedBy property
[user] in [models.User]
What did I do wrong?
In the mappedBy you need to use the name of the existing oposite field, and it's notification in your case - without 's' at the end.

Truncated table after execution of named Query with JPA

I've an issue with JPA and hibernate. I've created a named query in this class:
#Entity
#NamedQuery(name="findAllProjects", query="SELECT p FROM Project p")
#Table(name="projects")
public class Project {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="id")
private long id;
#Column(name="title")
private String title;
}
After execution of this query in this code lines my table is empty. What can be the reason?
List result = em.createNamedQuery("findAllProjects").getResultList();
Thanks and regards!