Exception in thread "main" javax.persistence.EntityNotFoundException: Unable to find CNPJ with id 00001388000307
I was reading jpa documentation, i read that this exception is thrown when it try to accesse(by the method getReference of EntityManger interface) and the entity doesn't exists
I have this entitys: Salesman
e CNPJ
. It's possible that exists many salesmen with the same CNPJ, in other words, a relationship @ManyToOne
.
This relationship is working, OK.
But, when i try to execute the query
select r from Salesman r join fetch r.yearMonth left join fetch r.cnpj
to bring the salesmen with its yearMonth(it's working!) relationship and its CNPJ relationship, throws the Exception, when i try to do a LEFT JOIN, that i mentioned.
When i don't execute a LEFT JOIN
, works great, all Salesmen with his CNPJs and nothin of Exceptions, BUUUUT, some salesmen don't have CNPJ nad i have to bring them too, there's a necessity to do a LEFT JOIN
.
@Entity
public class Salesman{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@JoinColumn(name = "year_month")
@ManyToOne
private YearMonth yearMonth;
private String cnpjS;
@JoinColumn(name = "cnpj")
@ManyToOne
private CNPJ cnpj;
public AnoMes getYearMonth() {
return yearMonth;
}
public CNPJ getCnpj() {
return cnpj;
}
}
@Entity
public class CNPJ {
@Id
@Column(name = "CCG", length = 14)
private String ccg;
public String getCcg() {
return ccg;
}
}
The select generated by Hibernate:
select *
from salesman s
inner join yearmonth y on s.ano_mes = y.id
left outer join cnpj c on s.cnpjS = c.CCG
This consult returns this values, rcnpj is cnpj from Salesman and bcnpj is from CNPJ. Some Salesmen came with CNPJ NULL, i think that it's the problem. I don't think so.
Try to enable hibernate sql logging with <property name="show_sql">true</property>
to see how real query (native, invoked on db) looks like, maybe there is a inner join after hibernate processing.
To change it you can try @Column(nullable=true)
annotation for your relationship or @Fetch(FetchMode.SELECT)
to see how real query will change.
The reason is that you have a reference to cnpj with ID 00001388000307, but such row of cnpj doesn't exist.
You can try to use @NotFound(action=NotFoundAction.IGNORE)
to skip such error, but it is recommended to fix your database of handle a exception.
You are using the cnpj column on the table Salesman as both a foreign key and a value column. In that case @staszko032's suggestion using @NotFound(action=NotFoundAction.IGNORE)
looks like will work as you expect
But I think you should refactor your model if possible. You could create a CNPJ entity always, in that case you won't have Salesman without a realation to CNPJ and could use INNER JOIN, or use 2 columns, one that will be used when the Salesman doesn't have a concrete related CNPJ (the value as string) and the other when it has (the foreign key)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.