title: Hibernate Model: set default @ManyToOne value of a Child Class
body: <p>I have class User which has @ManyToOne field Role.</p>
  
  <pre><code>@Entity 
  @Table(name="USERS")
  @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  @DiscriminatorValue("ROLE_ADMIN")
  @DiscriminatorColumn (name="ROLENAME", discriminatorType= DiscriminatorType.STRING, length=20)
  public class User extends BaseEntity implements UserDetails {
      // all others fields like username, password,...
  
      @ManyToOne
      @JoinColumn(name = "role_id", referencedColumnName="id") 
      @NotNull(message = "ROLE field is mandatory")
      private Role role;
  
      //getter and setter
  }
  </code></pre>
  
  <p>I have many classes that extends User: UserSeller, UserClient, UserOffice....</p>
  
  <pre><code>@Entity
      @DiscriminatorValue("ROLE_SELLER")
      @AttributeOverride(name = "role", column = @Column(name = "role_id"))
      public class UserSeller extends User {
  
         //additional fields like CompanyId,...
  
         //getter &amp; setter
      }
  </code></pre>
  
  <p>I have one panel where i can insert / edit / delete all kind of users,
  but i must have also "n" panel: one for each kind of user.</p>
  
  <p>When i'm in these panel, i would like to be able to insert these UserSeller without having
  to put a  where to choose role, but i want set this role as default.</p>
  
  <p>&bull; I tried to put a costructor in the UserSeller</p>
  
  <pre><code>@Entity
  @DiscriminatorValue("ROLE_SELLER")
  @AttributeOverride(name = "role", column = @Column(name = "role_id"))
  public class UserSeller extends User {
  
      @Transient
      RoleService roleService;
  
      @Transient
      User user = new User();
  
  
      public UserSeller()
      {
          super(); 
          try
          {
              this.setRole(roleService.getRole("ROLE_SELLER"));
          }
          catch (RecordNotFoundException e)
          {
  
          }
      }
  </code></pre>
  
  <p>but i get this error: </p>
  
  <pre><code>Could not get constructor for org.hibernate.persister.entity.SingleTableEntityPersister
  </code></pre>
  
  <p>&bull; i tried to pass a User object to constructor:</p>
  
  <pre><code>public UserSeller(User user)
  </code></pre>
  
  <p>and in controller i do this:</p>
  
  <pre><code> User user = new User();
   UserSeller seller = new UserSeller(user);
   model.addAttribute("seller", seller);
  </code></pre>
  
  <p>but i get this error:</p>
  
  <blockquote>
    <p>No default constructor for entity: com.machinet.model.UserVenditore</p>
  </blockquote>
  
  <ul>
  <li><p>If, in UserSeller i declare again the Role field i get error about
  "Redefined Column"....</p></li>
  <li><p>Finally i found this that i though could be my solution (in
  UserSeller class):</p>
  
  <pre><code>@PrePersist
  public void prePersist() {
          try
          {
              this.setRole(roleService.getRole("ROLE_SELLER"));
          }
          catch (RecordNotFoundException e)
          {
  
          }           
      }
  </code></pre></li>
  </ul>
  
  <p>but when i'm in the UserSeller panel and i try to add a new Seller, it doesnt' take the default role and the validation fails.</p>
  
  <p>I would like to know how can i do the trick: i want that my UserSeller, UserClient,... have a default value when i insert a new record. </p>
  
  <p>Do i really need to do this in the controller? is this the only way? because for a beginner like me, it doesn't look so so elegant solution:</p>
  
  <pre><code>UserVenditore venditore = new UserVenditore();
  try
  {
      venditore.setRole(roleService.getRole("ROLE_VENDITORE"));
  }
  catch (RecordNotFoundException ex)
  {
  
  }
  model.addAttribute("venditore", venditore);
  </code></pre>
  
  <p>edit: also this last solution doesn't work: it fails the validation!</p>
  
  <p>Thank you for any suggestion!</p>