Add a prototype of RegNoGenerator wich needs further tests
This commit is contained in:
parent
3d6941ab93
commit
a1bdae9e83
35
backend/src/main/java/ovh/herisson/Clyde/RegNoGenerator.java
Normal file
35
backend/src/main/java/ovh/herisson/Clyde/RegNoGenerator.java
Normal file
@ -0,0 +1,35 @@
|
||||
package ovh.herisson.Clyde;
|
||||
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.engine.jdbc.connections.spi.JdbcConnectionAccess;
|
||||
import org.hibernate.engine.spi.SessionImplementor;
|
||||
import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
||||
import org.hibernate.id.IdentifierGenerator;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
|
||||
public class RegNoGenerator implements IdentifierGenerator {
|
||||
@Override
|
||||
public Object generate(SharedSessionContractImplementor session, Object object) {
|
||||
try{
|
||||
JdbcConnectionAccess jdbccon = session.getJdbcConnectionAccess();
|
||||
Connection conn = jdbccon.obtainConnection();
|
||||
|
||||
Statement statement = conn.createStatement();
|
||||
String query = "select count(reg_no)+1 from Users";
|
||||
|
||||
ResultSet set = statement.executeQuery(query);
|
||||
|
||||
if (set.next()){
|
||||
return set.getLong(1)+1000;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -1,6 +1,8 @@
|
||||
package ovh.herisson.Clyde.Tables;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
@ -8,7 +10,8 @@ import java.util.Date;
|
||||
@Table(name = "Users")
|
||||
public class User {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
@GenericGenerator(name = "userGen", type = ovh.herisson.Clyde.RegNoGenerator.class)
|
||||
@GeneratedValue(generator = "userGen")
|
||||
private Long regNo;
|
||||
private String lastName;
|
||||
private String firstName;
|
||||
|
Loading…
Reference in New Issue
Block a user