简体   繁体   中英

Database Query working on DBMS directly but not in Java

I am trying to do things before processing Ingest to KIT DataManager ( Code on GitHub , it runs on tomcat7) with a "Staging Processor" …

adding a custom Staging Processor

package edu.kit.dama.mdm.content.mets;

public class TryQuota extends AbstractStagingProcessor {

@Override
public final void performPreTransferProcessing(TransferTaskContainer pContainer) throws StagingProcessorException {

trying to get user data

… this works

        UserData userResult = null;
        try {
            userResult = mdm.findSingleResult(
                        "Select u FROM UserData u WHERE u.email=?1",
                        new Object[]{"dama@kit.edu"},

standard email of admin user with userid 1

                        UserData.class
                    );
        } catch (UnauthorizedAccessAttemptException e2) {
            System.out.println("exception on extracting userid");
                    e2.printStackTrace();
        }
        try {
            System.out.println("KIT DM ID: " + userResult.getUserId());
        }catch(Exception e4) {
            System.out.println("exception on output for userid");
            e4.printStackTrace();
        }

trying to get quota from UserQuota

and on the other hand, the corresponding implementation doesn't do the job here (that I want to get working)

        Number UserQuota = null;
        try {
            UserQuota = mdm.findSingleResult(
 //SQL would be: "SELECT quota FROM userquota WHERE uid=?;"
 //JPQL is …
                    "Select q.quota FROM UserQuota q WHERE q.uid=?1",
                    new Object[]{1},
                    Number.class
            );
        } catch (UnauthorizedAccessAttemptException e2) {
            System.out.println("exception on userquota");
            e2.printStackTrace();
        }
        System.out.println("quota is: " + UserQuota );

UserQuota is still null here

DB is PostgreSQL, Table is:

CREATE SEQUENCE userquota_seq       
    START WITH 1
    INCREMENT BY 1
    NO MAXVALUE
    NO MINVALUE;

CREATE TABLE userquota
( 
   id INTEGER NOT NULL DEFAULT nextval('userquota_seq'),
   uid INTEGER NOT NULL DEFAULT 0,
   quota DECIMAL NOT NULL DEFAULT 0,        
   CONSTRAINT uid_key UNIQUE (uid), 
   CONSTRAINT fk_uid FOREIGN KEY (uid) REFERENCES users(id)     
);

This quota I want to get from db in the processor

INSERT INTO userquota (id, uid,quota) VALUES ( 0, 1, 1048576 );

So mainly I want to get the entry for the ingesting user (here 1) from db: 1048576 as a Long.

Any hints welcome on how to proceed on these things.

As stated in above comment , the following statement is invalid SQL syntax:

SELECT u FROM UserData u WHERE u.email=?1

Instead, according to above comment the correct syntax would be:

SELECT u.* FROM UserData u WHERE u.email='dama@kit.edu'

I found a solution by creating a class UserQuota 1 so JPQL can work here. As I did not find a way around, I first copied class UserData , dropped everything I had no use of and changed the members according to my database table userquota . This also means when extending the db table this class needs to be altered as well.

A very important part I had no clue about is that this new class also needs to be registered for tomcat7 in persistence.xml , also here copied over from the entry for UserData .

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