简体   繁体   中英

JPA Query in inheritance tables

i'm new on JPA, i need some help with a query that i would like to make.

This are my entities:

Service Entity

@Entity
@Table(name = "service", schema = "db2")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "serviceType")
public abstract class Service implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int idService;
    @ManyToMany
    @JoinTable(name = "pack-serv", schema = "db2", joinColumns = @JoinColumn(name = "idService"), inverseJoinColumns = @JoinColumn(name = "idPackage"))
    private List<Package> packages;

    public Service() {
    }
    //getter and setter of all attributes
}

Fixed Inte.net entity

@Entity
@Table(name = "service", schema = "db2")
@DiscriminatorValue("Fixed Internet")
public class FixedInternet extends Service implements Serializable  {
    private static final long serialVersionUID = 1L;
    
    private int GB;
    private int feeExtraGB;
    @Column(name="serviceType", insertable = false, updatable = false)
    protected String type;
    
    public FixedInternet() {
        super();
    }
     //getter and setter of all attributes
}

I would like to retrieve all FixedInte.net services that are related to a specific package ID. I made this query:

@NamedQuery(name = "FixedInternet.findServicesByPackID", query = "SELECT s.GB, s.feeExtraGB, s.idService FROM FixedInternet s JOIN s.packages p WHERE p.idPackage = ?1 AND s.type LIKE 'Fixed Internet'")

but when i run it an error shows up related to SQL syntax:

Internal Exception: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-serv t2, db2.package t1 WHERE ((((t1.IDPACKAGE = 1) AND t0.serviceType LIKE 'Fi' at line 1
Error Code: 1064
Call: SELECT t0.GB, t0.FEEEXTRAGB, t0.IDSERVICE FROM db2.service t0, db2.pack-serv t2, db2.package t1 WHERE ((((t1.IDPACKAGE = ?) AND t0.serviceType LIKE ?) AND (t0.serviceType = ?)) AND ((t2.idService = t0.IDSERVICE) AND (t1.IDPACKAGE = t2.idPackage)))
    bind => [3 parameters bound]

I can't resolve it, someone can help me to understand what i'm wrong?

This is the ER Diagram of the database if it could help: ER Diagram

UPDATE : without the part of "s.type LIKE 'Fixed Inte.net'", the error is pretty the same:

Internal Exception: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-serv t2, db2.package t1 WHERE ((((t1.IDPACKAGE = 1) AND t0.serviceType LIKE 'Fi' at line 1
Error Code: 1064
Call: SELECT t0.GB, t0.FEEEXTRAGB, t0.IDSERVICE FROM db2.service t0, db2.pack-serv t2, db2.package t1 WHERE (((t1.IDPACKAGE = ?) AND (t0.serviceType = ?)) AND ((t2.idService = t0.IDSERVICE) AND (t1.IDPACKAGE = t2.idPackage)))
    bind => [2 parameters bound]

I had the same error when I tried to CREATE a Table.

Internal Exception: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER (ID BIGINT AUTO_INCREMENT NOT NULL, DATE DATE, ORDERTYPE INTEGER, VERSION ' at line 1

Error Code: 1064

Call: CREATE TABLE ORDER (ID BIGINT AUTO_INCREMENT NOT NULL, DATE DATE, ORDERTYPE INTEGER, VERSION INTEGER, CUSTOMER_ID BIGINT, PRIMARY KEY (ID))

The problem was that I was using a word reserved for MySQL (ORDER) to name the Table. The solution was to change the Table name to other.

If this helps...

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM