0

Im using SequenceGenerator and GeneratedValue with sequence "seq_user_hierarchy" to do a "select nextval('NAME_OF_SEQUENCE')" in the ID field.

Is there any possibility to use a custom query like "select value from NAME_OF_VIEW" instead of the default query?

Is there any other way to insert the result of my custom query in the id field?

Thanks in advance

/**
* User hierarchy JPA entity.
*/
@Entity
@Table(name="USER_HIERARCHY")
@Data
@AllArgsConstructor
@RequiredArgsConstructor
@Builder
@EqualsAndHashCode(of={"id"},callSuper=false)
@ToString(of={"id"})
public class UserHierarchy extends AuditableEntity{

/**
 * User hierarchy id.
 * 
 * @param id New value for this User hierarchy's id.
 * @return The current value of this User hierarchy's id.
 */

@SequenceGenerator(name="seq_user_hierarchy", sequenceName="seq_user_hierarchy")
@Id
@GeneratedValue(generator="seq_user_hierarchy")
private Long id;


/**
 * User hierarchy user.
 * 
 * @param id New value for this User hierarchy's user.
 * @return The current value of this User hierarchy's user.
 */
@ManyToOne
@JoinColumn(name = "USER_ID")
User user;

/**
 * User hierarchy team.
 * 
 * @param id New value for this User hierarchy's team.
 * @return The current value of this User hierarchy's team.
 */
@OneToOne
@JoinColumn(name="TEAM_ID")
SalesTeam team;

/**
 * User hierarchy branch.
 * 
 * @param id New value for this User hierarchy's branch.
 * @return The current value of this User hierarchy's branch.
 */
@OneToOne
@JoinColumn(name="BRANCH_ID")
SalesBranch branch;

/**
 * User hierarchy area.
 * 
 * @param id New value for this User hierarchy's area.
 * @return The current value of this User hierarchy's area.
 */
@OneToOne
@JoinColumn(name="AREA_ID")
SalesArea area;

/**
 * User hierarchy region.
 * 
 * @param id New value for this User hierarchy's region.
 * @return The current value of this User hierarchy's region.
 */
@OneToOne
@JoinColumn(name="REGION_ID")
SalesRegion region;

/**
 * User hierarchy is team leader.
 * 
 * @param id New value for this User hierarchy's is team leader.
 * @return The current value of this User hierarchy's is team leader.
 */
Boolean isTeamleader;

@Temporal(TemporalType.TIMESTAMP)
private Date startDate;

@Temporal(TemporalType.TIMESTAMP)
private Date endDate;

}
  • https://stackoverflow.com/questions/31158509/how-to-generate-custom-id-using-hibernate-while-it-must-be-primary-key-of-table Maybe this can helps you out. – Roni Koren Kurtberg Nov 29 '19 at 10:16

1 Answers1

0

Please try the following code changes and confirm if it works.

public class UserHierarchy extends AuditableEntity {

...

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "author_generator")
@SequenceGenerator(name="author_generator", sequenceName = "author_seq")
@Column(name = "id", updatable = false, nullable = false)
private Long id;

...

}

Explanation to the above code snippet:

  1. In @GeneratedValue annotation, generator attribute is the logical name which is defined in the following annotation.
  2. In @SequenceGenerator annotation, sequenceName attribute is the actual name of the database sequence, name attribute is the resolution to the above mentioned logical name of the generator

Moreover, these links (Link1 Link2) may help you.

Varun Jain
  • 1,371
  • 12
  • 26
  • Thanks Varum, Im sorry, I dont understand how this in going to let me do a "select value from NAME_OF_VIEW" and inserts it on the ID field. – Carlos Martínez Romero Nov 29 '19 at 11:47
  • For that, I believe you can follow the link which is shared by Roni Koren Kurtberg in the comment to your query. So which will allow you to access to code part where you select nextvalue as well as other things. – Varun Jain Nov 29 '19 at 13:42