Caused by: java.sql.SQLSyntaxErrorException: [SQL0205] Column MITMAS_MMCONO not in table OOLINE in schema - spring

I have 3 entity classes
Ooline:
#Entity
#IdClass(OolineId.class)
#NamedQuery(name="Ooline.findAll", query="SELECT o FROM Ooline o")
public class Ooline implements Serializable
{
private static final long serialVersionUID = 1L;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumns({
#JoinColumn(name="mitmas_mmcono" , referencedColumnName="mmcono"),
#JoinColumn(name="mitmas_mmitno" , referencedColumnName="mmitno")
})
private Mitmas mitmas ;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumns({
#JoinColumn(name="mitfac_m9cono", referencedColumnName="m9cono"),
#JoinColumn(name="mitfac_m9itno", referencedColumnName="m9itno"),
#JoinColumn(name="mitfac_m9faci", referencedColumnName="m9faci")
})
private Mitfac mitfac ;
#Id
private String obitno;
#Id
private BigDecimal obcono;
#Id
private String obfaci;
private BigDecimal obabno;
//getter and setters
}
Mitfac:
#Entity
#IdClass(MitfacId.class)
#NamedQuery(name="Mitfac.findAll", query="SELECT m FROM Mitfac m")
public class Mitfac implements Serializable
{
private static final long serialVersionUID = 1L;
#OneToMany(mappedBy="mitfac",fetch=FetchType.LAZY)
private List<Ooline> oolineItem;
#Id
private String m9itno;
#Id
private String m9faci;
#Id
private BigDecimal m9cono;
private String m9acrf;
private BigDecimal m9appr;
//getter and setter
}
Mitmas:
#Entity
#IdClass(MitmasId.class)
#NamedQuery(name="Mitmas.findAll", query="SELECT m FROM Mitmas m")
public class Mitmas implements Serializable
{
private static final long serialVersionUID = 1L;
#OneToMany(mappedBy="mitmas",fetch=FetchType.LAZY)
private List<Ooline> ooline;
#Id
private String mmitno;
#Id
private BigDecimal mmcono;
private BigDecimal mmaad0;
private BigDecimal mmaad1;
private String mmaccg;
//getter and setter methods
}
And following is my Repository file:
public interface OolineRepository extends JpaRepository<Ooline, String>
{
public static final String ORDER_LINE_QUERY =
"select x.obcono,x.oborno,x.obponr, x.obposx, x.obfaci, x.obitno, x.obitds, x.oborqt, "
+ "x.oborqa, x.obwhlo, x.obadid, x.obpopn, x.obrout, x.obmodl, x.oborst,"
+ " x.obnepr, x.obdwdz, x.obcodz, x.obpldt, x.obplhm,"
+ " x.obprrf, x.obspun, x.obrgdt, x.obrgtm, x.obwhsl from Ooline x "
+ "left join x.mitmas y left join x.mitfac z "
+ "where x.obcono = :cono and x.oborno = :orno";
#Query(ORDER_LINE_QUERY)
public List<Ooline> getOolineLines(#Param("cono") BigDecimal cono, #Param("orno") String orno);
}
I am supposed to execute following query in SQL
"select x.oborno,x.obponr, x.obposx, x.obfaci, x.obitno, x.obitds, x.oborqt, x.oborqa,
x.obwhlo, x.obadid, x.obpopn, x.obrout, x.obmodl,x.oborst, x.obnepr, x.obdwdz, x.obcodz,
x.obpldt, x.obplhm,x.obprrf, x.obspun, x.obrgdt, x.obrgtm, x.obwhsl from Ooline x join
schema.mitmas on mmcono = obcono and mmitno = obitno left join schema.mitfac on
m9cono = obcono and m9faci = obfaci and m9itno = obitno where obcono =1 and oborno = 'asasa'";
Can anyone verify if have constructed and mapped entity correctly .
But query is not constructing the way I am expecting .Getting an error "Column MITMAS_MMCONO not in table OOLINE ." Can anyone help me in this

The mapped entity is correct constructed, but the query is not prpeely defined. You want a left join from child enity Ooline to master entity Mitmas, and it tries to do that by joining on the primary key fields from Ooline to those from Mitmas (see generated sql) which is not correct. Why do you want a left join since you are not actually using anything from the master entity?

I have changed as following
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumns({
#JoinColumn(updatable=false,insertable=false,name="obcono",referencedColumnName="mmcono"),
#JoinColumn(updatable=false,insertable=false,name="obitno",referencedColumnName="mmitno")
})
private Mitmas mitmas ;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumns({
#JoinColumn(updatable=false,insertable=false,name="obcono",referencedColumnName="m9cono"),
#JoinColumn(updatable=false,insertable=false,name="obitno",referencedColumnName="m9itno"),
#JoinColumn(updatable=false,insertable=false,name="obfaci",referencedColumnName="m9faci")
})
private Mitfac mitfac ;

Related

Get data from multiple tables using one Mapping Table in Spring Boot

Procedure.java
#Entity
#Table(name = "procedures")
#JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class Procedure implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "ProcedureId")
private int id;
#Column(name = "ProcedureName")
private String name;
#Column(name = "ProcedureCode")
private String code;
#Column(name = "ProcedureDesc")
private String desc;
// getters and setters
}
CPTClinicianDescriptor
#Entity
#Table(name = "cliniciandescriptor")
public class CPTClinicianDescriptor {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "Id")
private int id;
#Column(name = "ConceptId")
private int conceptId;
#Column(name = "CPTCode")
private String cptCode;
#Column(name = "ClinicianDescriptorId")
private int clinicianDescriptorId;
#Column(name = "ClinicianDescriptor")
private String clinicianDescriptor;
// getters and setters
}
CPTProcedures
#Entity
#Table(name = "cptprocedures")
public class CPTProcedures {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "Id")
private int Id;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "ProcedureId")
private Procedure procedure;
#Transient
private int procedureId;
#OneToOne(fetch=FetchType.EAGER)
#JoinColumn(name = "CPTCodeSet")
private CPTClinicianDescriptor cptClinicianDescriptor;
#Transient
private int cptCodeSet;
// getters and setters
}
Here I have 3 table in mySql in those 2 are different separate tables, I created one table for Mapping that has foreign keys of Procedures and CPT when i get a list using Procedure It should get corresponding cpt data also.
ServiceImpl Like:
public List<Procedure> getCombinedProcedureList(int id) {
// TODO Auto-generated method stub
List<Procedure> pList = new ArrayList<Procedure>();
Procedure procedureList = procedureRepository.findCombById(id);
pList.add(procedureList);
return pList;
}
When i use
Repository like
#Query(value = "SELECT * FROM procedures JOIN cliniciandescriptor ON cliniciandescriptor.Id = ?1",nativeQuery = true)
Procedure findCombById(int id);
this query in Procedures Repository it is getting only procedures. So, what can i do to get the combined(joined) list of 2(procedures & CPT) tables using 3rd mapping table.

SQL to JPQL, How to query Nested JPQL

I wonder if JPQL can be nested query. I am studying Spring Data JPA, and I also have uploaded several related questions.
If I have below sql in MySQL, how do I produce JPQL:
select
c.*
from
cheat c
left join (select * from cheat_vote where val = 1) v on c.cheat_seq = v.cheat_fk
group by
c.cheat_seq
having
count(*) < 10
limit 5
I have two entities.
public class Cheat implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "cheat_seq", length = 10)
private Long cheatSeq;
#Column(name = "question", unique = true, nullable = false)
private String question;
#Column(name = "answer", unique = true, nullable = false)
private String answer;
#Column(name = "writer_ip", nullable = false)
private String writerIP;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "reg_date", nullable = false)
private Date regDate;
#Transient
private String regDateText;
#OneToMany(mappedBy = "cheat", fetch=FetchType.LAZY)
private Set<CheatVote> vote;
#Override
public String toString() {
return "Cheat [cheatSeq=" + cheatSeq + "]";
}
}
Above entity has a #OneToMany collection, and the collection entity is below.
public class CheatVote implements Serializable{
private static final long serialVersionUID = 1L;
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Id
#Column(name="seq", nullable=false)
private Long seq;
#Column(name="val", nullable=false)
#NonNull
private Integer value;
#Column(name="ip_address", nullable=false)
#NonNull
private String ipAddress;
#JoinColumn(name="cheat_fk", referencedColumnName="cheat_seq")
#ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
#NonNull
private Cheat cheat;
#Override
public String toString() {
return "CheatVote [seq=" + seq + "]";
}
}
I want to get Cheat entitiy which has less than 10 children CheatVote entities.
You can try it:
#Query("SELECT c FROM Cheat c LEFT JOIN c.vote v WHERE v.value = 1 GROUP BY c.cheatSeq HAVING count(c) < 10")
About 'LIMIT' you can use parameter Pageable of Spring Data JPA

QuerySyntaxException in Spring Data JPA custom query

I have a Entity:
#Entity
#Table(name = "story", schema = "")
#Data
public class Story implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "sID", unique = true, nullable = false)
private Long sID;
#Column(name = "vnName", nullable = false)
private String vnName;
#Temporal(TemporalType.TIMESTAMP)
#DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss")
#Column(name = "sUpdate", length = 19)
private Date sUpdate;
}
And:
#Entity
#Table(name = "chapter", schema = "")
#Data
public class Chapter implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "chID", unique = true, nullable = false)
private Long chID;
#Column(name = "chName", nullable = false)
private String chName;
#JoinColumn(name = "sID", referencedColumnName = "sID")
#ManyToOne(fetch = FetchType.LAZY)
private Story story;
}
I had created custom pojo to get the latest update story with the latest chapter:
#Data
public class NewStory{
private Story story;
private Chapter chapter;
}
but when I get list :
#Repository
public interface StoryRepository extends CrudRepository<Story, Long> {
#Query(value="SELECT NEW com.apt.truyenmvc.entity.NewStory(s as newstory, c as newchapter)"
+ " FROM story s LEFT JOIN (SELECT * FROM Chapter c INNER JOIN "
+ " (SELECT MAX(c.chID) AS chapterID FROM Story s LEFT JOIN Chapter c ON s.sID = c.sID GROUP BY s.sID) d"
+ " ON c.chID = d.chapterID) c ON s.sID = c.sID order by s.sUpdate desc")
public List<NewStory> getTopView();
}
Error:
Warning error: org.hibernate.hql.internal.ast.QuerySyntaxException: story is not mapped.
Who could help me fix it? Or could it be done in a different way?
The error is pretty self explainatory. And its just a typo in your query. You are using story. And obviously thats not mapped as an Entity.
Fix it to Story

Spring JPA: How to insert data to join many tables with #ManytoMany relationship

I'm starting to learn Spring Java Framework . I created some Enity to join 2 Model like my Database. And now I want to insert to Join Table by JpaRepository. What i have to do?
This is my Code (Please fix help me me if something is not right)
Model Users_RoomId to define Composite Primary Key
#Embeddable
public class Users_RoomId implements Serializable {
private static final long serialVersionUID = 1L;
#Column(name = "ID_room", nullable = false)
private String idRoom;
#Column(name = "user_id", nullable = false)
private int idUser;
}
Model Users_Room to join 2 Model Users and Room
#Entity
#Table(name ="bookroom")
public class Users_Room {
#EmbeddedId
private Users_RoomId usersroomId;
#ManyToOne
#MapsId("idRoom")
private Room room;
#ManyToOne
#MapsId("idUser")
private Users users;
#Column(name = "Bookday")
private String bookday;
Model Users and Room I used annotation #OneToMany
Model Users
#Entity
#Table(name = "users")
public class Users implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "user_id", nullable = false)
private int id;
#Column(name = "name", nullable = false)
private String name;
#Column(name = "email")
private String email;
#Column(name = "pass")
private String pass;
#Column(name = "role")
private int role;
#OneToMany(mappedBy = "users")
private List<Users_Room> user;
Model Room
#Entity
#Table(name ="room")
public class Room implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "ID_room", nullable = false)
private String id;
#Column(name = "name_room", nullable = false)
private String name;
#Column(name = "Description")
private String describe;
#ManyToOne
#JoinColumn(name = "ID_status")
private Status status;
#Column(name = "room_image")
private String image;
public Room() {
super();
}
#ManyToOne
#JoinColumn(name = "ID_kind")
private KindRoom kind;
#OneToMany(mappedBy = "room")
private List<Users_Room> rooms;
This is my database
So I don't know how to insert a new bookroom with iduser,idroom and bookday with JPA repository.. It'necessary to write Query in JPARepository or We just need to use method save() to insert data
Thanks everyone
I had same problem and solved with following code. I used method save() to insert data. Following code is 'createRoom' method in 'RoomService.java'.
RoomService.java
private final RoomRepository roomRepository;
private final UserRoomRepository userRoomRepository;
private final UserRepository userRepository;
public RoomService(RoomRepository roomRepository, UserRoomRepository userRoomRepository, UserRepository userRepository) {
this.roomRepository = roomRepository;
this.userRoomRepository = userRoomRepository;
this.userRepository = userRepository;
}
#Transactional
public RoomDto createRoom(Long userId, Long chattingUserId) {
Room room = roomRepository.save(new Room());
room.addUserRoom(userRepository.findById(userId).orElseThrow(()->new NoSuchElementException("No User")));
room.addUserRoom(userRepository.findById(chattingUserId).orElseThrow(()->new NoSuchElementException("No User")));
userRoomRepository.save(new UserRoom(userRepository.findById(userId).orElseThrow(()->new NoSuchElementException("No User")),room));
userRoomRepository.save(new UserRoom(userRepository.findById(chattingUserId).orElseThrow(()->new NoSuchElementException("No User")),room));
RoomDto roomDto = RoomDto.of(room);
return roomDto;
}

Hibernate session closed with large volume

I try to retrieve the data from a table linked to a parent table with the following entities:
Entity
#Table(name = "REF_BONJOUR_UNITE")
#NamedQuery(name = "UniteEntity.findAll", query = "SELECT u FROM UniteEntity u")
public class UniteEntity implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "CODE_UNITE")
private String codeUnite;
// bi-directional many-to-one association to Insee
#OneToMany(mappedBy = "refBonjourUnite", fetch = FetchType.LAZY, cascade = CascadeType.MERGE )
private List<Insee> refBonjourInsees;
and the daughter entity :
#Entity
#Table(name = "REF_BONJOUR_INSEE")
#NamedQuery(name = "Insee.findAll", query = "SELECT i FROM Insee i")
public class Insee implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
private InseePK id;
#Temporal(TemporalType.DATE)
#Column(name = "DATE_FIN_INSEE_GEO")
#IsOracleDate
private Date dateFinInseeGeo;
private String tylemt;
// bi-directional many-to-one association to UniteEntity
#ManyToOne
#JoinColumn(name = "CODE_UNITE_FK", insertable = false, updatable = false)
private UniteEntity refBonjourUnite;
When i try to access this list with a large amount of data, i get a failed to lazily initialize a collection of role, here exactly :
List<Insee> codeInsees = unite.getRefBonjourInsees();
Insee codeInsee = codeInsees.get(0)

Resources