#Post request, Constructors & primary keys

1 messages · Page 1 of 1 (latest)

dusty junco
#

My question is:
when creating the class, my class has an (autogenerated) ID. My constructor as well, when I had it this way I could not create a POST request because I couldn’t fill in the ID argument, because this should be auto-generated. So I removed it from the constructor but I kept it as an attribute of the class. Is this the right way? Because I’ve seen an aproach online of someone creating 2 constructors one with and one without the id in it.

My second question is. One of my attributes is a date, so I used the LocalData type for the variable. But when creating a POST request through postman (by giving JSON object) it put the value null in the date column/object. How am I supposed to fill in the date? I tried it “DD-MM-YYYY” but it gave the null.

cedar abyssBOT
#

<@&987246584574140416> please have a look, thanks.

deep hatch
#

Your creation DTO shouldn't have an ID, so the approach online isn't correct either. And do you have jackson-datatype-jsr310?

rugged cobalt
#

make records for your request/response shapes and just manually convert

#

skips this whole nightmare

dusty junco
valid parcel
#

LocalDate is YYYY-MM-DD because it's based on ISO8601. If you're using spring it should have everything setup for you

dusty junco
valid parcel
#

Spring boot?

dusty junco
#

yea, post through postman to test the post method

valid parcel
#

did you do any configuration of the objectmapper?

dusty junco
#

No I haven't yet, maybe I will come across it further in the tutorial I am following

#

Also when creating a table, it won't put it in the db, only if I add a table name. Why is that?

valid parcel
#

I should have to look at the code to tell you that

deep hatch
dusty junco
#

For me this was not the case, but through stackoverflow I found spring.jpa.properties.hibernate.globally_quoted_identifiers=true this line fixed it for me, but I have no clue if this is a correct way.

rugged cobalt
dusty junco
#

Yes

rugged cobalt
#

well thats your issue

#

for that one in particular use @Table("'user'")

#

or set that property

#

some dbs don't like user as a table name in all situations

#

but i need to stress again, please don't use that entity class for a response or request body

#

you will continue to encounter confusing, surprising, horrible, things

dusty junco
#

Besides that I have UserDTO which I use

dusty junco
rugged cobalt
dusty junco
#

I don't have it yet but for User I have OrderDTO, here is the OrderDTO:

package com.pump.webshop.DTO;

import java.time.LocalDate;

public class OrderDTO {
    public int userID;
    public int paymentID;
    public String orderStatus;
    public LocalDate date;

    public OrderDTO(int userID, int paymentID, String orderStatus, LocalDate date) {
        this.userID = userID;
        this.paymentID = paymentID;
        this.orderStatus = orderStatus;
        this.date = date;
    }
}

Order class

package com.pump.webshop.models;

import jakarta.persistence.*;

import java.time.LocalDate;

@Table(name = "orders")
@Entity
public class Order {
    private @Id @GeneratedValue long id;
    private int userID;
    private int paymentID;
    private String orderStatus;
    private LocalDate orderDate;


    public Order() {
    }

    public Order(int userID, int paymentID ,String orderStatus, LocalDate orderDate) {
        this.userID = userID;
        this.paymentID = paymentID;
        this.orderStatus = orderStatus;
        this.orderDate = orderDate;
    }

I was planning to do the same with order

cedar abyssBOT
rugged cobalt
#
public record OrderDTO(int userID, int paymentID, String orderStatus, LocalDate date) {
}
#

if you are on a newer java

dusty junco
#

I amon 21

rugged cobalt
#

then do that for your DTO-type stuff

#

it will make it so if you print it out it will have a good toString, if nothing else

dusty junco
rugged cobalt
#

example

#
class Main {
    public static void main(String[] args) {
        System.out.println(new Apple("red"));
    }
}

class Apple {
    public String color;
    
    Apple(String color) {
        this.color = color;
    }
}
#
Apple@55b0dcab
#
#
class Main {
    public static void main(String[] args) {
        System.out.println(new Apple("red"));
    }
}

record Apple(String color) {}
#
Apple[color=red]
#
#

you can add a toString method to any class to customize how it is printed, records just get one which includes all the fields by default

#

among other things

dusty junco
rugged cobalt
#

yeah