0

how can execute a sequence in JpaRepository?

and then read the id from the Entity.

    public interface EmailRepository extends JpaRepository<Email, Long> {

    }

    emailService.persistEmail(from, to, subject, content);
        @Inject
        private EmailRepository emailRepository;

        @Transactional
        public void persistEmail(String mailFrom, String mailTo, String subject, String content) {
            Email email = new Email(mailFrom, mailTo, content, subject);
            Email persisted = emailRepository.save(email);
            persisted.getId();
            log.debug("Persisted Information for Mail: {}", persisted.getMailFrom());
        }

@Data
@Entity
@NoArgsConstructor
@EqualsAndHashCode(of = "id")
@Table(name = "mails")
public class Email {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "mail_from", nullable = false, length = 250)
    private String mailFrom;

    @Column(name = "mail_to", nullable = false, length = 250)
    private String mailTo;

    @Column(name = "mail_subject", nullable = false, length = 200)
    private String subject;

    @Column(name = "mail_content", nullable = false, length = 5000)
    private String content;

    public Email(String mailFrom, String mailTo, String subject, String content) {
        this.mailFrom = mailFrom;
        this.mailTo = mailTo;
        this.subject = subject;
        this.content = content;
    }
}

i can't persist because the id is null. Any ideas. Also how can i ignore create a constructor in lambook without id

  • What does the entity class `Email` look like? Does it have a `Long id` field? Does that field have an `@Id` and `@GeneratedValue` annotation? – Jesper Dec 01 '17 at 13:49
  • can you attach the code of the `Email` class? – Ron Badur Dec 01 '17 at 13:50
  • i mean to lambook to create a constructor for all arguments, but not for the id –  Dec 01 '17 at 14:09
  • Your RDBMS supports autoincrement/serial columns? That is what `IDENTITY` is using, but you dont say which RDBMS –  Dec 01 '17 at 18:09
  • I'm using postgre –  Dec 01 '17 at 20:17
  • and the table was created with what DDL? Did you let the JPA provider create it? And `IDENTITY` is not a "Sequence" (title of the question), it is an autoincrement field. I'd expect you to have type "SERIAL" for the "ID" column –  Dec 02 '17 at 06:47
  • The idea is jparepository to create the id sequence which I should get from the entity and use it to create a something like uid from this id, encode it and use for insert in mail_from like part of the mail 1234&%$%@domain.com. And idea is to persist in one transaction. –  Dec 02 '17 at 10:22

2 Answers2

0

I think you are missing a label for the Id. Have you try using the @TableGenerator label like the following?

@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.TABLE, generator="EMAIL_SEQ")
@TableGenerator(name="EMAIL_SEQ", table="SequenceKey", pkColumnName="SeqKey", pkColumnValue="EmailBean", valueColumnName="Seq" )
private Long id;
Mikel Dalmau
  • 80
  • 1
  • 7
  • I have tried your example, but no success, any other ideas –  Dec 01 '17 at 14:55
  • Sorry, you should change the @Generatedvalue label also, I corrected the answer. – Mikel Dalmau Dec 01 '17 at 14:57
  • ' @Id @Column(name = "id") @GeneratedValue(strategy=GenerationType.TABLE, generator="MAILS_SEQ") @TableGenerator(name="MAILS_SEQ", table="mails", pkColumnName="id", pkColumnValue="id" ) private Long id; ' is it correct? seems like it is not working –  Dec 01 '17 at 15:30
  • Maybe if can give more info about your aplication context...are you using spring? Still I don't think you should change the pkColumnName or pkColumnValue. I use the same pattern for all my domain classes. – Mikel Dalmau Dec 01 '17 at 15:39
  • CREATE TABLE public.mails ( id integer NOT NULL, mail_from character varying(250) NOT NULL, mail_to character varying(250) NOT NULL, mail_subject character varying(200) NOT NULL, mail_content character varying(5000) NOT NULL, sent_date timestamp(6) without time zone NOT NULL DEFAULT now(), CONSTRAINT pk_mails PRIMARY KEY (id) –  Dec 01 '17 at 15:46
  • org.postgresql.util.PSQLException: ERROR: invalid input syntax for integer: "id" Position: 40 –  Dec 01 '17 at 15:48
0

See here

How to set auto increment primary key in PostgreSQL?

and here

JPA and PostgreSQL with GenerationType.IDENTITY

If you want to use @GeneratedValue(strategy = GenerationType.IDENTITY) then your ID column needs to be of type SERIAL and not integer as it currently is.

Alan Hay
  • 22,665
  • 4
  • 56
  • 110