How would be a good practice to create a new standard record for entities?

Asked

Viewed 11 times

-1

How good would be a practice to create a new default record of the entities below?

I need this because front-end requirements require at least one record for an editable table to be shown.

I thought of creating a method in the Service where I instate my entity using builders, but when faced with relationship between entities I noticed that this did not seem to be a good practice.

service method:

 public User  insertDefaultEntity(){
       User userDefault = new User("nome padrão", objEnderecoPadrao, (etc));
       return userService.insert(userDefault)
    }

User Identity with Address Relationship:

   @Entity
    @Table(name = "users")
    public class User {

        public User(String name, Address adrress){
          this.name = name;
          //...
        }
        
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id")
        private Long id;
    
        private String name;
        //... 
    
        @OneToOne(cascade = CascadeType.ALL)
        @JoinColumn(name = "address_id", referencedColumnName = "id")
        private Address address;
    
        // ... getters and setters
    }

Entity Address:

@Entity
@Table(name = "address")
public class Address {

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

    //... getters and setters
}

Would anyone have any more elegant suggestions than that?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.