How generate two sequence in one table hibernate - mysql

I use spring boot/jpa/hibernate/MYSQL to generate schema and have table orders. I want generate 2 sequence in one table. First generated second ignored. How I could achieve that?
#Entity
#Data
#Table(name = "orders")
public class Orders {
#Id
#GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
#Column(name = "order_number")
#GeneratedValue(strategy= GenerationType.AUTO)
private Long orderNumber;
}

The implementation of #GeneratedValue with the AUTO strategy in your actual SQL database will likely be to use an auto increment column. Most databases do not even allow having two auto increment columns, and in general, there should not be a need why you even would need this. I suggest just deleting one of either the id or orderNumber fields, and keeping the other one as the only auto increment column in your entity/table.

Related

Tables sharing id column in springboot with mysql

I hava a spring-boot application that makes CRUD operations on a mysql datebase.
In thid db, there are two tables 'Employee' and 'Employee_departament' (both with auto-increment primary key and employee_id is foreign key in Employee_departament).
When I add a new employee in Employee table it will generate a record with id = 1. After that, if I add a new record in Employee_departament, this one will have id = 2.
This looks like my tables are sharing id column.
If you are using #GeneratedValue(strategy = GenerationType.SEQUENCE) above the id fields in both of your entities then they will both use the default database sequence (named hibernate_sequence) to generate new ids. This results in the behavior that you are describing.
If you want the entities to have their ids generated from separate sequences, you can specify the name of the sequence to get the ids by specifying the generator value inside of the GeneratedValue annotation. For example:
Employee.java
#Entity
#Table(name = "employee")
public class Employee {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_seq")
private Long employee_id;
...
EmployeeDepartment.java
#Entity
#Table(name = "employee_department")
public class EmployeeDepartment {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_department_seq")
private Long employee_department_id;
...
Please add these two annotations into your entity class id attribute
#GeneratedValue(strategy = GenerationType.AUTO)
#Access(AccessType.PROPERTY)

Unknown column 'this_.id' in 'field list' when fetching With Hibernate and MySql

I am begginer in Hibernate and i have a table named suffrages1 with no relations to the rest of the database.
The table has an autoincrement identifier field named Suffrages_id.
In the corresponding entity, i am declaring the field as follows :
#Entity(name = "suffrages1")
public class Suffrage1 implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
#Id
#JoinColumn(name = "Suffrages_id")
#GeneratedValue(strategy = GenerationType.AUTO)
private int id;
The insertion is fine but when i try to fetch i get this error :Unknown column 'this_.id' in 'field list'
All similar issues i found were about problems with relationships between tables or about correct column names for mapping but as i said this table has no relationship with other table and i think i have the correct names e,g i know that my Java variable in the class is named id but is annotated with #JoinColumn to correspond to the Mysql column. So where did 'this_.id' come from ?
Sorry, i was using #JoinColumn instead of #Column to define the id field.

Unknown table 'hibernate_sequence' in field list

I have strange error here...
I have two databases configurated on this project, and when i try to save into local mysql repository, i get the title error.
In addition i have remote oracle db in use.
Hibernate:
select
hibernate_sequence.nextval
from
dual
and then
[nio-8080-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown table 'hibernate_sequence' in field list
and there is no table named hibernate.sequence in database, or attribute in class.
#Id
#GeneratedValue
long id;
#Column(name = "customerid")
private String customerid;
#OneToMany(targetEntity = C_Portfolio.class, fetch = FetchType.LAZY, cascade = {CascadeType.ALL}, orphanRemoval=true)
#Fetch(FetchMode.SELECT)
private List<C_Portfolio> portfolios;
#Column(name = "date")
private LocalDate date;
#Column(name = "date_time")
private LocalDateTime datetime;
Furthermore everything seems to be okey just before the save. When i check the class to be saved in debugger mode. it has all the needed values and everything seems to be okey.
You have simply used #GeneratedValue in the POJO, so it tries to find the sequence table. Since you haven't specified the sequence table name, it will look for default sequence table "hibernate_sequence".
For mysql it increments the value by specifying as like below in your POJO:
#GeneratedValue(strategy = GenerationType.AUTO)
If you use it, it will generate a table called hibernate_sequence to provide the next number for the ID sequence.

How to give Jpa #GeneratedValue(strategy = GenerationType.AUTO) a start value instead of 0

I have an entity that is supposed to get an id from the database automatically. I use MySQL so I would expect annotating that #GeneratedValue(strategy=GenerationType.AUTO) When i run the project i will import hardcoded master data needed for my application, but when i add a new data to same table using jpa it starts with id 0 thus saying id already exist.
I have to make jpa start auto increment from the last id i have added . Is this possible
Use the GenerationType.TABLE approach here in order to feed the data based on what value already present in your database.
We can use #SequenceGenerator to specify the start sequence.
#Entity
#Table(name = "employee_data")
#SequenceGenerator(name="employee_id_seq", initialValue=5, allocationSize=100)
public class EmployeeDO {
#Id
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="employee_id_seq")
private Long id;

hibernate not generate auto increment constraint on mysql table

I have been searching through different forums for information and I have tried different solutions, but I'm still unable to correct my issue.
I am using hibernate4 annotations for mapping my entities. Auto increment key is not detected when tables are created using hibernate in mysql.
I have the following code:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(unique = true, nullable = false)
private int responseId;
I have also tried:
#Id
#GenericGenerator(name="generator", strategy="increment")
#GeneratedValue(generator="generator")
private int responseId;
With hibernate the id is automatically assigned to a row, but in the mysql table it has no AutoIncrement Constraint. I have to mark field as AI manually. This becomes problematic when I manually insert a record for testing or use jdbc statements for the table. Please let me know what I am missing in configuration that is preventing the hibernate id from assigning an AutoIncrement Contraint.
Use the IDENTITY generator, and use the columnDefinition attribute of #Column to specify the type of the column:
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(columnDefinition = "MEDIUMINT NOT NULL AUTO_INCREMENT")
private int responseId;