I am unable to reset the id of the objects that I am adding to SQL, through JPA. I tried to write a native query to truncate when I use my deleteAll method, which works, no errors, but the autogenerated id is still not 1.
I tried "ALTER TABLE task AUTO_INCREMENT = 1" after deleting, same result.
My method used for resetting
#Transactional
#Modifying
#Query(value = "ALTER TABLE task AUTO_INCREMENT = 1", nativeQuery = true)
void resetId();
My entity id
public class Task {
#Id
#Column(name = "id", nullable = false)
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
Related
How can I create a Single Entity from two other tables/Entity in JPA/hibernate making a INNER JOIN result with a common key. I was using the below code but it gives me a full join instead of an inner join. it give me records from the meal table even if the
"id 1" does not exist in the allergies table, example:
{id=1, name='tacos', description='Mexican food', price ='10',peanuts=null, celery=null, sesameSeeds=null}
How can constrain to don't return any records if the 'id' is missing from the secondary table allergies? to show only records when the primary key is present in both tables.
I want something like this instead:
{id=1, name='tacos', description='Mexican food', price ='10',peanuts='no', celery='no', sesameSeeds='no'}
Please advise.
#Entity
#Table(name = "meal")
#SecondaryTable(name = "allergens", pkJoinColumns = #PrimaryKeyJoinColumn(name = "meal_id"))
class Meal {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id")
Long id;
#Column(name = "name")
String name;
#Column(name = "description")
String description;
#Column(name = "price")
BigDecimal price;
#Column(name = "peanuts", table = "allergens")
boolean peanuts;
#Column(name = "celery", table = "allergens")
boolean celery;
#Column(name = "sesame_seeds", table = "allergens")
boolean sesameSeeds;
// standard getters and setters
}
MySQL Version = 5.5.51
SpringBoot Version = 1.2.8.RELEASE
mysql-connector-java = 5.1.36
Given:
I have an entity which has an index on a column(deliveryIndicator) whose data type is "bit NOT NULL DEFAULT 0".
I have set ddl-auto to update in my application.properties like this:
spring.jpa.hibernate.ddl-auto = update
When
When I ran my application
Then
JPA reset the all the data for the deliveryIndicator column to 0.
Note: other index column data are not resetting.
My entity is defined like this
#Entity
#Table(name = "Office",
indexes = {#Index(name = "geo_index", columnList = "latitude,longitude"),
#Index(name = "deliveryIndicator", columnList = "deliveryIndicator")})
public class Office {
#Id
#Column(name = "id")
private long id;
#Column(name = "longitude")
private Double longitude;
#Column(name = "latitude")
private Double latitude;
#Column(name = "parkingIndicator", columnDefinition = "bit NOT NULL DEFAULT 0")
private boolean parkingIndicator;
#Column(name = "disabilityIndicator", columnDefinition = "bit NOT NULL DEFAULT 0")
private boolean disabilityIndicator;
#Column(name = "deliveryIndicator", columnDefinition = "bit NOT NULL DEFAULT 0")
private boolean deliveryIndicator;
}
Now, I have tried following to fix it:
Removed the index on deliveryIndicator.
#Entity
#Table(name = "Office",
indexes = {#Index(name = "geo_index", columnList = "latitude,longitude")})
public class Office {....}
After this when I ran the application (after bringing back the DB to the previous state) the deliveryIndicator column data was not effected/resetted.
My query is :
Why this is happening.
Is there any other solution for this.
I´m doing a Web-Application with a Spring Boot Backend and an AngularJS frontend.
Database is MySQL.
I try do get the Top 5 Items out of my database. I´m doing this through the
#Query Annotation. I tried my SQL Statement directly onto the DB and i get the expected values.
SQL Statement:
select item
from work_item
group by item
order by count(*) desc
limit 3
#Query:
#Query(value="SELECT WORK_ITEM_ITEM FROM WORK_ITEM GROUP BY WORK_ITEM_ITEM ORDER BY COUNT( *) DESC LIMIT 5", nativeQuery = true)
List< String > find5MostUsedItems();
When i try to access the related URL i´m getting the following error:
java.lang.IllegalArgumentException: PersistentEntity must not be null!
Would be glad if someone could push me into the right direction.
In case you need more information, plz ask. (My first question :) )
Edit: my Entity:
#Entity
public class WorkItem {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "workItem_ID")
private long id;
#Column(name = "workItem_date")
private LocalDate date;
#Column(name = "workItem_mitarbeiterNummer")
private int mitarbeiterNummer;
#Column(name = "workItem_startTime")
private LocalTime startTime;
#Column(name = "workItem_endTime")
private LocalTime endTime;
#Column(name = "workItem_workDuration")
private int workDuration;
#Column(name = "workItem_item")
private String item;
#Column(name = "workItem_itemDescription")
private String itemDescription;
Greetings!
I think you should try SELECT WORKITEM_ITEM instead of SELECT WORK_ITEM_ITEM
I am using Sprint Boot, Sprint Data JPA, Hibernate 4 and MySQL. I have a table with an autoincrement column. I can insert new entities ok and the column auto-increments. Sometimes though I want to specify the ID when doing an insert but Hibernate appears to ignore any ID value I set.
Table definition:
#Entity
#Table(name = "example")
public class Example implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "id")
private Integer id;
... more columns ....
}
How can I have both auto-increment when no id is set AND allow setting of the ID by my application?
Its because your ID is not that will be stored in the database,rather than that it is considering #GeneratedValue(strategy = GenerationType.IDENTITY) which is generating values for you
I have multiple entities annotated with the following:
#TableGenerator(name = "XXX_Gen", table = "XXX_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL")
#GeneratedValue(strategy = GenerationType.TABLE)
#Id
private String id;
I am using MySQL behind the scenes with eclipselink and the problem is that regardless of the values that I enter in the name and table MySQL always just uses a single table called 'SEQUENCE' to increment the PK's values.
Normally this isn't an issue except I have a specific case where I need an entity to have its own incremental sequence.
I wasn't specifying the generator! It should be;
#TableGenerator(name = "XXX_Gen", table = "XXX_GEN", pkColumnName = "GEN_NAME", valueColumnName = "GEN_VAL")
#GeneratedValue(strategy = GenerationType.TABLE, generator="XXX_Gen")
#Id
private String id;
I think I need another cup of coffee!