I am working on a relationship in spring jpa and facing this issue. I am trying to establish relationship between a subuseraccess table and subuservehicleaccess table.
Now a subuser can have access to multiple vehicles and each vehicle will have a set of access privileges for the sub user.(eg: read,write).
The private List<SubUserVehicleAccessFeatureList>
property in the first class defines the list of the relation class added later viz SubUserVehicleAccessFeatureList
.
Error
I am trying to insert record in these tables and, while I am getting an error sql error 1364 sqlstate hy000 hibernate
saying the field subUserAccessFeatureId
doesn't have a default value.
I tried to debug and the data while saving is all correct. On the contrary if I remove the @JoinColumn(name = "SUBUSERACCESSFEATUREID"))
annotation from the below property private List<SubUserVehicleAccessFeatureList>
it works as expected. As a part of convention, we need to have names without underscores and hibernate generates underscored names by default.
Referred this but didn't find a solution.
@Entity
@Table(name = "SUBUSERACCESSFEATURELIST")
public class SubUserAccessFeatureList extends TableMetaDataDTO {
/** The sub user access feature id. */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SUBUSERACCESSFEATUREID")
private Long subUserAccessFeatureId;
/** The sub user id. */
@Column(name = "SUBUSERID")
private Long subUserId;
/** The vehicle id. */
@Column(name = "VEHICLEID")
private Long vehicleId;
/** The vehicle access feature list. */
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinTable(name = "SUBUSERVEHICLEACCESSFEATURELIST", joinColumns = @JoinColumn(name =
"SUBUSERACCESSFEATUREID"))
private List<SubUserVehicleAccessFeatureList> vehicleAcccessFeatureList = new ArrayList<SubUserVehicleAccessFeatureList>();
/** The account access feature list. */
@Column(name = "FEATUREID")
@JoinTable(name = "SUBUSERACCOUNTACCESSFEATURELIST", joinColumns = @JoinColumn(name = "SUBUSERID"))
@ElementCollection(fetch = FetchType.LAZY)
private Set<Long> accountAccessFeatureList = new HashSet<Long>();
//Getter setter eliminated
}
The relation class
@Entity
@Table(name = "SUBUSERVEHICLEACCESSFEATURELIST")
public class SubUserVehicleAccessFeatureList {
/** The sub user vehicle feature id. */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "SUBUSERVEHICLERELATIONID")
private Long subUserVehicleFeatureId;
/** The vehicle id. */
@Column(name = "VEHICLEID")
private Long vehicleId;
/** The feature id. */
@Column(name = "FEATUREID")
@JoinTable(name = "SUBUSERVEHICLERELATIONTABLE", joinColumns = @JoinColumn(name = "SUBUSERVEHICLERELATIONID"))
@ElementCollection(fetch = FetchType.LAZY)
private Set<Long> featureId = new HashSet<Long>();
}