0

I try to create a Student entity in java using JPA, but something not work.

I have a table in postgresql named "Studenti" and class Student

My student class is:


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;

@Entity(name = "Student")
@Table(name = "Studenti")
public class Student implements Serializable {
    @Id
    @Column(name = "Id")
    private long Id;

    @Column(name = "Nume")
    private String firstName;

    @Column(name = "Prenume")
    private String lastName;

    @Column(name = "An")
    private int year;

    @Column(name = "Id_cont")
    private long accountId;
}

And the table has the following structure:

enter image description here

Application.properties:

server.port=9091

spring.application.name=user-management

spring.datasource.url=jdbc:postgresql://localhost:5432/Licenta
spring.datasource.username=postgres
spring.datasource.password=*****

spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true

Need to crete a special file with setting for this or something else ?

2
  • A uuid is not a long. I would suspect that this is the problem. Can you please include the stack trace you encounter? Commented Dec 22, 2019 at 12:21
  • please specify @GeneratedValue for the id column. Commented Dec 22, 2019 at 12:27

1 Answer 1

1

Nope, you can configure PostgreSQL connection only in application.properties file

Maybe few more configuration parameters, especially this

spring.datasource.driver.class=org.postgresql.Driver

If you already have a DB structure, better set ddl-auto=none

And PostgreSQL has Oracle engine (or like Oracle engine) so for @Id field better to use sequences

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "genname")
@SequenceGenerator(name = "genname", sequenceName = "seqname", allocationSize = 1)

And @Id field must not be a primitive, just Long, Integer, String, UUID or other possible implementation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.