Mysql - JPA no insert into thrid table Many to Many - java

I have Many-To-Many relation in my project, i can write in my two Entities table, the relational table does not get anything written.
EspecificacionEscenario Class:
public class EspecificacionEscenario implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idespecificacionEscenario")
private Integer idespecificacionEscenario;
#Column(name = "codigo")
private String codigo;
#Column(name = "fecha")
#Temporal(TemporalType.TIMESTAMP)
private Date fecha;
#Column(name = "nombreProceso")
private String nombreProceso;
#Column(name = "nombreEscenario")
private String nombreEscenario;
#Column(name = "objetivoEscenario")
private String objetivoEscenario;
#Column(name = "lugarEscenario")
private String lugarEscenario;
#Column(name = "recursoEscenario")
private String recursoEscenario;
#Column(name = "restriccionEscenario")
private String restriccionEscenario;
#Column(name = "actoresEscenario")
private String actoresEscenario;
#ManyToMany(mappedBy = "especificacionEscenarioList", fetch = FetchType.LAZY)
private List<Elicitacion> elicitacionList;
#ManyToMany(mappedBy = "especificacionEscenarioList", fetch = FetchType.LAZY)
private List<Episodio> episodioList;
#ManyToMany(mappedBy = "especificacionEscenarioList", fetch = FetchType.LAZY)
private List<Educcion> educcionList;
Episodio class:
public class Episodio implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idepisodio")
private Integer idepisodio;
#Column(name = "codigo")
private String codigo;
#Column(name = "objetivoEpisodio")
private String objetivoEpisodio;
#Column(name = "descripcionEpisodio")
private String descripcionEpisodio;
#Column(name = "recursosEpisodio")
private String recursosEpisodio;
#Column(name = "restriccionEpisodio")
private String restriccionEpisodio;
#Column(name = "actor")
private String actor;
#JoinTable(name = "especificacionEscenarioEpisodio", joinColumns = {
#JoinColumn(name = "idepisodio", referencedColumnName = "idepisodio")}, inverseJoinColumns = {
#JoinColumn(name = "idespecificacionEscenario", referencedColumnName = "idespecificacionEscenario")})
#ManyToMany(fetch = FetchType.LAZY)
private List<EspecificacionEscenario> especificacionEscenarioList;
Main code:
public static void main(String[] args) {
EpisodioDao episodioDao = new EpisodioDao();
EspecificacionEscenarioDao escenarioDao = new EspecificacionEscenarioDao();
Episodio episodio = new Episodio();
episodio.setCodigo("e01");
episodio.setDescripcionEpisodio("descripcion episodio");
EspecificacionEscenario ee = new EspecificacionEscenario();
ee.setCodigo("ee-01");
List<Episodio> listaE = new ArrayList<>();
listaE.add(episodio);
ee.setEpisodioList(listaE);
episodioDao.registrarEpisodio(episodio);
System.exit(0);
}
when doing the persistence in the entities the information is saved automatically, but in the table third table it does not insert the primary keys.

I have added CascadeType.ALL on Episodio as it is the owner of this relation.
Following code may help you. I have tested with spring data jpa.
#Setter
#Getter
#Entity
public class Episodio implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idepisodio")
private Integer idepisodio;
#Column(name = "codigo")
private String codigo;
#Column(name = "objetivoEpisodio")
private String objetivoEpisodio;
#Column(name = "descripcionEpisodio")
private String descripcionEpisodio;
#Column(name = "recursosEpisodio")
private String recursosEpisodio;
#Column(name = "restriccionEpisodio")
private String restriccionEpisodio;
#Column(name = "actor")
private String actor;
#JoinTable(name = "especificacionEscenarioEpisodio",
joinColumns = {
#JoinColumn(name = "idepisodio", referencedColumnName = "idepisodio")},
inverseJoinColumns = {
#JoinColumn(name = "idespecificacionEscenario", referencedColumnName = "idespecificacionEscenario")})
#ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<EspecificacionEscenario> especificacionEscenarioList;
}
#Setter
#Getter
#Entity
public class EspecificacionEscenario implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Basic(optional = false)
#Column(name = "idespecificacionEscenario")
private Integer idespecificacionEscenario;
#Column(name = "codigo")
private String codigo;
#Column(name = "fecha")
#Temporal(TemporalType.TIMESTAMP)
private Date fecha;
#Column(name = "nombreProceso")
private String nombreProceso;
#Column(name = "nombreEscenario")
private String nombreEscenario;
#Column(name = "objetivoEscenario")
private String objetivoEscenario;
#Column(name = "lugarEscenario")
private String lugarEscenario;
#Column(name = "recursoEscenario")
private String recursoEscenario;
#Column(name = "restriccionEscenario")
private String restriccionEscenario;
#Column(name = "actoresEscenario")
private String actoresEscenario;
#ManyToMany(mappedBy = "especificacionEscenarioList", fetch = FetchType.LAZY)
private List<Episodio> episodioList;
}
EspecificacionEscenario especificacionEscenario = new EspecificacionEscenario();
especificacionEscenario.setCodigo("ee-01");
List<EspecificacionEscenario> especificacionEscenarios = new ArrayList<>();
especificacionEscenarios.add(especificacionEscenario);
Episodio episodio = new Episodio();
episodio.setCodigo("e01");
episodio.setDescripcionEpisodio("descripcion episodio");
episodio.setEspecificacionEscenarioList(especificacionEscenarios);
episodioRepo.save(episodio);

Related

Id declaration in Spring

I have some entity classes and i have a question because I want to clear it in my mind. In case of #ManyToOne relationship where I insert for example #JoinColumn(name = "cardHolderId"), shall I remove the primitive private Long cardHolderId? Because I had a discussion and a programmer explained to me that I can avoid declaring. But if I avoid it I can not use it in test cases like services.
#Entity
#Getter
#Setter
#Builder
#NoArgsConstructor
#AllArgsConstructor
#Table(name = "card")
public class Card {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
#Column(name = "code")
private String code;
#Column(name = "number")
private String number;
#Column(name = "issueDate")
private Date issueDate;
#Column(name = "expireDate")
private Date expireDate;
#Column(name = "elasticDate")
private Date elasticDate;
#Column(name = "pin")
private Long pin;
#Column(name = "isValid")
private Boolean isValid;
#Column(name = "isUsed")
private Boolean isUsed;
#Column(name = "isPin")
private Boolean isPin;
#Column(name = "cardCategoryId")
private Long cardCategoryId;
#Column(name = "hasNumberOfVisits")
private Boolean hasNumberOfVisits;
#Column(name = "numberOfVisits")
private Long numberOfVisits;
#Column(name = "isBlackListed")
private Boolean isBlackListed;
#Column(name = "cardHolderId")
private Long cardHolderId;
//Check Relationships
#ManyToOne
#JoinColumn(name = "cardCategoryId")
private CardCategory cardCategory;
#ManyToOne
#JoinColumn(name = "cardHolderId")
private CardHolder cardHolder;
#Column(name = "companyGroupId")
private Long companyGroupId;
#ManyToOne
#JoinColumn(name = "companyGroupId")
private CompanyGroup companyGroup;
#OneToMany(mappedBy = "card")
private List<AccessControlSubject> accessControlSubjects = new ArrayList<>();
#OneToMany(mappedBy = "card")
private List<Card2Role> card2Roles = new ArrayList<>();
#ManyToOne
#JoinColumn(name = "cardHistoryId")
private CardHistory cardHistory;
#OneToMany(mappedBy = "vehicleCard")
private List<Vehicle> vehicles = new ArrayList<>();
#ManyToOne
#JoinColumn(name = "sharedCardId")
private PatrolSharedCard sharedCard;
#ManyToOne
#JoinColumn(name = "productionWorkId")
private PrdWork productionWork;
#ManyToOne
#JoinColumn(name = "productionWorkHitsId")
private PrdWorkHits productionWorkHits;
#OneToMany(mappedBy = "card")
private List<VisitorCardHistory> visitorCardHistories = new ArrayList<>();
You can remove it and use the id from the referred entity :
this.getCardHolder().getId()

How to ignore update or insert join if NULL value

I have a entity bean with a relation #ManyToOne that is in join on one column.
#Entity
#Table(name = "work_order")
public class WorkOrder implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator")
private Long id;
#NotNull
#Column(name = "id_order", nullable = false)
private String idOrder;
#Column(name = "description")
private String description;
#Enumerated(EnumType.STRING)
#Column(name = "status")
private StatusOrder status;
#Column(name = "creation_date")
private Instant creationDate;
#Column(name = "closing_date")
private Instant closingDate;
#Column(name = "client_id")
private Long clientId;
#ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST) #NotFound (action = NotFoundAction.IGNORE)
#Fetch(FetchMode.JOIN)
#JoinColumn(name = "account", insertable = false, updatable = false, nullable = true)
private AnagraficaClienti account;
And the second Entity
#Entity
#Table(name = "es_account")
public class AnagraficaClienti implements Serializable {
private static final long serialVersionUID = 1L;
// da rimettere a #NotNull
#Column(name = "fk_cod_azienda", nullable = true)
private String fk_cod_azienda;
#Id
#NotNull
#Column(name = "account", nullable = false)
private String account;
// da rimettere a #NotNull
#Column(name = "tipo_cli_for", nullable = true)
private String tipoClienteFornitore;
#Column(name = "tipo_account", nullable = true)
private String tipoAccount;
....
The "es_account" table has three not nullable primary key(fk_cod_azienda, account, tipo_cli_for) and the relation with the "work_order" table is by account column.
My problem is that sometimes it is possible that the user insert or update WorkOrder with a null account value and that is not avoid by AnagraficaClienti entity because it expects a non null(and not duplicate) value.
Are there any possible way to bypass the join with AnagraficaClienti when account is null?
In my point of view, #ManyToOne is violate OOP design principle due to the creation of redundant relation. Instead, i always create a #OneToMany relation with a list of related entities. To specify the relation as nullable, just add the nullable=true property in #JoinColumn. With #ManyToOne, you must specify property optional=true. Lets try and see if it works.
WorkOrder
#Entity
#Table(name = "work_order")
public class WorkOrder implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator")
private Long id;
#NotNull
#Column(name = "id_order", nullable = false)
private String idOrder;
#Column(name = "description")
private String description;
#Enumerated(EnumType.STRING)
#Column(name = "status")
private StatusOrder status;
#Column(name = "creation_date")
private Instant creationDate;
#Column(name = "closing_date")
private Instant closingDate;
#Column(name = "client_id")
private Long clientId;
AnagraficaClienti
#Entity
#Table(name = "es_account")
public class AnagraficaClienti implements Serializable {
private static final long serialVersionUID = 1L;
// da rimettere a #NotNull
#Column(name = "fk_cod_azienda", nullable = true)
private String fk_cod_azienda;
#Id
#NotNull
#Column(name = "account", nullable = false)
private String account;
// da rimettere a #NotNull
#Column(name = "tipo_cli_for", nullable = true)
private String tipoClienteFornitore;
#Column(name = "tipo_account", nullable = true)
private String tipoAccount;
#OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
#JoinColumn(name = "account", nullable = true)
private List<WorkOrder> workOrders;
When you want to insert the work order to the database:
workOrderRepository.save(workOrder);
When you want to create the relationship:
AnagraficaClienti client = anagraficaClientiRepository.findById(...);
client.getWorkOrders().add(newWorkOrder);

How to negate several predicates

I have two entities
#Entity
#Table(name = SIGNAL")
public class TradingSignal {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ID", nullable = false, unique = true)
private int id;
#Column(name = "SIGNAL_ID")
private int signalId;
#Column(name = "TICKER", length = 6)
private String ticker;
#OneToMany(cascade = CascadeType.ALL)
private Set<TsUsed> tsUsedSet;
}
and
#Entity
#Table(name = "TS_USED")
public class TsUsed {
#Id
#Column(name = "FILTER_ID", nullable = false)
#GeneratedValue(strategy = GenerationType.AUTO)
private int filterId;
#Column(name = "USER_ID_LSB")
private long userIdLsb;
#Column(name = "USER_ID_MSB")
private long userIdMsb;
#Column(name = "VISIBLE")
private boolean visible;
#ManyToOne(cascade = CascadeType.ALL)
#JoinColumn(name = "ID", nullable = false)
private TradingSignal tradingSignal;
}
How can I find all TradingSignals where TsUsed.userIdLsb and TsUsed.userIdMsb are not exists in tsUsedSet?
Can't create valid predicate.
This code not work for me
Join<TradingSignal, TsUsed> tsUsed = getTsUsedJoin(root);
Predicate predicateUserIdLsb = builder.equal(tsUsed.get("userIdLsb"), userId.getLeastSignificantBits());
Predicate predicateUserIdMsb = builder.equal(tsUsed.get("userIdMsb"), userId.getMostSignificantBits());
Predicate predicateInvisible = builder.isFalse(tsUsed.get("visible"));
Predicate notInvisibleForUser = builder.and(predicateUserIdLsb, predicateUserIdMsb, predicateInvisible).not();

JHipster Spring boot : org.hibernate.HibernateException: Unable to access lob stream

I created my app using JHipster. When i try to get list of tournaments via TournamentQueryService i get this error :
Exception in TournamentQueryService.findByCriteria() with cause =
'org.hibernate.HibernateException: Unable to access lob stream' and
exception = 'Unable to access lob stream; nested exception is
org.hibernate.HibernateException: Unable to access lob stream'
This is filter and Page object :
find by criteria : TournamentCriteria{}, page: Page request [number:
0, size 8, sort: startDate: DESC]
So it just gets 8 first tournaments.
This is tournament class :
#Entity
#Table(name = "tournament")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#Document(indexName = "tournament")
public class Tournament extends AbstractAuditingEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
#SequenceGenerator(name = "sequenceGenerator")
private Long id;
#Column(name = "name")
private String name;
#Column(name = "location")
private String location;
#Column(name = "url")
private String url;
#Column(name = "start_date")
private ZonedDateTime startDate;
#Column(name = "end_date")
private ZonedDateTime endDate;
#Column(name = "entry_fee")
private Double entryFee;
#Column(name = "prize")
private Double prize;
#Column(name = "goods")
private String goods;
#Column(name = "favorite_rating")
private Long favoriteRating;
#Column(name = "participants_number")
private Integer participantsNumber;
#Column(name = "finished")
private Boolean finished;
#Column(name = "view_only")
private Boolean viewOnly;
#Column(name = "image")
private String image;
#Column(name = "description")
private String description;
#Column(name = "teams_applied")
private String teamsApplied;
#Lob
#Column(name = "schedule")
private String schedule;
#Lob
#Column(name = "prize_distribution")
private String prizeDistribution;
#Lob
#Column(name = "contacts")
private String contacts;
#Lob
#Column(name = "rules")
private String rules;
#OneToMany(mappedBy = "tournament", fetch = FetchType.LAZY)
#JsonIgnore
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<Stream> streams = new HashSet<>();
#ManyToMany(fetch = FetchType.EAGER)
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#JoinTable(name = "tournament_platforms", joinColumns = #JoinColumn(name = "tournaments_id", referencedColumnName = "id"), inverseJoinColumns = #JoinColumn(name = "platforms_id", referencedColumnName = "id"))
private Set<Platform> platforms = new HashSet<>();
#ManyToOne
private Game game;
#ManyToOne
private TournamentStatus status;
#ManyToOne(fetch = FetchType.LAZY)
private EntryType entryType;
#ManyToOne(fetch = FetchType.LAZY)
private TournamentFormat format;
#ManyToOne
private Region region;
#ManyToOne(fetch = FetchType.LAZY)
private GameMode gameMode;
#ManyToOne(fetch = FetchType.LAZY)
private PrizeType prizeType;
#ManyToOne
private Organizer organizer;
#ManyToOne(fetch = FetchType.LAZY)
private TournamentStage stage;
#ManyToOne
private HostPlatform hostPlatforms;
#ManyToOne(fetch = FetchType.LAZY)
private TournamentType type;
#ManyToOne
private PlayType playType;
#ManyToOne
private Currency currency;
#ManyToOne
private Country country;
Here is the method that calls hibernate :
#Transactional(readOnly = true)
public Page<Tournament> findByCriteria(TournamentCriteria criteria, Pageable page) {
log.info("find by criteria : {}, page: {}", criteria, page);
final Specifications<Tournament> specification = createSpecification(criteria);
Page<Tournament> result = tournamentRepository.findAll(specification, page);
return result;
}
Is it possibile that you are trying to access Lob properties when hiberante session is closed?
Try to replace your #Lob properties with the following:
#Basic(fetch=FetchType.EAGER) #Lob
and check if the error persists.

ORA-01400 cannot insert null error in one to one relationship

i have this code
public void guardarAspirante(AspiranteDTO aspiranteDTO) {
Aspirante aspirante = new Aspirante();
String usuarioMovimiento = AspiranteCN.class.getSimpleName();
Date fecha = new Date();
aspirante.setCodigoAlumno(aspiranteDTO.getCodigoUniversitario());
aspirante.setNombre(aspiranteDTO.getNombre());
aspirante.setApellidoPaterno(aspiranteDTO.getPrimerApellido());
aspirante.setApellidoMaterno(aspiranteDTO.getSegundoApellido());
aspirante.setUsuarioMovimiento(usuarioMovimiento);
aspirante.setFechaMovimiento(fecha);
Solicitud solicitud = new Solicitud(aspirante.getSolicitudId());
solicitud.setAspirante(aspirante);
solicitud.setSolicitudId(aspirante.getSolicitudId());
solicitud.setOfertaId(aspiranteDTO.getOfertaAcademica());
solicitud.setPeriodoId(aspiranteDTO.getPeriodo());
solicitud.setAportacion(aspiranteDTO.getAportacionVoluntaria());
solicitud.setFechaMovimiento(fecha);
solicitud.setUsuarioMovimiento(usuarioMovimiento);
aspirante.setSolicitud(solicitud);
....
aspiranteDAO.persist(aspirante);
}
and this error
Internal Exception: java.sql.SQLException: ORA-01400: cannot insert NULL into ("RPINGRE"."ARE_SOLI"."ARE_SOLI_SOLIASPI_ID")
This is Aspirante Entity (Fragment)
#Entity
#Table(name = "ARE_SOLIASPI", catalog = "", schema = "RPINGRE")
public class Aspirante implements Serializable {
private static final long serialVersionUID = 1L;
#SequenceGenerator(sequenceName = "RPINGRE.SQ_ARE_SOLIASPI",
name = "RPINGRE.SQ_ARE_SOLIASPI",
initialValue = 1,
allocationSize = 1)
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "RPINGRE.SQ_ARE_SOLIASPI")
#Id
#Basic(optional = false)
#Column(name = "ARE_SOLIASPI_ID")
private Long solicitudId;
#Basic(optional = false)
#Column(name = "ARE_SOLIASPI_CODIGO")
private String codigoAlumno;
#Basic(optional = false)
#Column(name = "ARE_SOLIASPI_NOMBRE")
private String nombre;
#Column(name = "ARE_SOLIASPI_APE_PATERNO")
private String apellidoPaterno;
#Column(name = "ARE_SOLIASPI_APE_MATERNO")
private String apellidoMaterno;
#Basic(optional = false)
#Column(name = "ARE_SOLIASPI_MOV_USUARIO")
private String usuarioMovimiento;
#Basic(optional = false)
#Column(name = "ARE_SOLIASPI_MOV_FECHA")
#Temporal(TemporalType.TIMESTAMP)
private Date fechaMovimiento;
#OneToOne(cascade = CascadeType.ALL, mappedBy = "aspirante", fetch = FetchType.LAZY)
private Solicitud solicitud;
and Solicitud Entity (Fragment)
#Entity
#Table(name = "ARE_SOLI", catalog = "", schema = "RPINGRE")
public class Solicitud implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#Column(name = "ARE_SOLI_SOLIASPI_ID")
private Long solicitudId;
#Basic(optional = false)
#Column(name = "ARE_SOLI_MOV_USUARIO")
private String usuarioMovimiento;
#Basic(optional = false)
#Column(name = "ARE_SOLI_MOV_FECHA")
#Temporal(TemporalType.TIMESTAMP)
private Date fechaMovimiento;
#Column(name = "ARE_SOLI_PERIODO_ID")
private String periodoId;
#Column(name = "ARE_SOLI_OFERTA_ID")
private Long ofertaId;
#Column(name = "ARE_SOLI_APORTACION")
private Long aportacion;
#JoinColumn(name = "ARE_SOLI_SOLIASPI_ID", referencedColumnName = "ARE_SOLIASPI_ID", insertable = false, updatable = false, nullable = false)
#OneToOne(optional = false, fetch = FetchType.LAZY)
private Aspirante aspirante;
.....
}
try changing GenerationType.SEQUENCE to GenerationType.Auto.
The ORA-01400 error says that you are trying to insert a NULL into a column defined as NOT NULL.
I suggest you ether set a default value on the column or have your code make sure the NOT NULL columns have data before you do the INSERT (or use an NVL function in your INSERT statement)

Categories

Resources