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!
Related
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")
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.
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?
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
}
I have 2 tables with #ManyToMany relation field. In hibernate cfg i have
<property name="hbm2ddl.auto">update</property>
Table which is created during application startup has UNIQUE key set on PartId column, which is
#JoinColumn(name="PartId")}
in #ManyToMany relation. I didn't set anywhere that this column should have unique key. Is this the default auto creation behaviour?
The DB is MySQL 5.5
Thanks.
UPD:
Full field desc is:
#ManyToMany
#JoinTable(name="Part_Dev",
joinColumns={#JoinColumn(name="PartId")},
inverseJoinColumns= {#JoinColumn(name="DevCode")})
public List<Dom> getDom() { return dom; }
UPD 2
sorry, I see I didn't mention it. Unique key in Parts table,
#Entity #Table(name="Parts")
public class Parts implements Serializable{
#ManyToMany
#JoinTable(name="Part_Dev",
joinColumns={#JoinColumn(name="PartId")},
inverseJoinColumns= {#JoinColumn(name="DevCode")})
public List<Dom> getDom() {
return dom; }
#Column(name="PartId")
public Integer getPartId() {
return partId; }
you need to specify #JoinTable to make it happen. For instance, if you have two entities : Employee and Project in a many-to-many relationship. You need to have #JoinTable on one side.
#Entity
public class Employee {
#Id private int id;
private String name;
#ManyToMany
#JoinTable(name="EMP_PROJ",
joinColumns=#JoinColumn(name="EMP_ID"),
inverseJoinColumns=#JoinColumn(name="PROJ_ID"))
private Collection<Project> projects;
So, as Chris told, that was the way to identify each part.