Insert a statement with QueryDSL

2,969 views
Skip to first unread message

Norman Cash

unread,
Sep 18, 2013, 1:29:49 PM9/18/13
to quer...@googlegroups.com
I am developing an application using QueryDSL.
I would like to insert data into a table

I try to do this with SQLInsertDML. How can I do it?
Actually we are composing the text that contains insert instruction for example :

INSERT INTO TABLA1
(campo1,campo2)
values
(50,25);

I did not  find  an example over the internet.



Timo Westkämper

unread,
Sep 18, 2013, 1:33:13 PM9/18/13
to Querydsl on behalf of Norman Cash
Here are the steps
* create the Querydsl metamodel (Q* classes)
* use one of the SQLTemplates subclasses that fits your SQL database
* create a SQLInsertClause and populate it
* execute it

All of this is covered in the reference docs.

Br,
Timo


--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Timo Westkämper
Mysema Oy
+358 (0)40 591 2172
www.mysema.com


Norman Cash

unread,
Sep 18, 2013, 1:51:36 PM9/18/13
to quer...@googlegroups.com
Apparently addresses an application that does the same as the ANT to create the Q * classes, in our case the insert instruction is contained in one class.
Eg. Class Exchange.
name = tablename.
List <Campos> listCampos = fields.

I do not think we could do in a static one QCLASS.

Norman Cash

unread,
Sep 19, 2013, 11:14:08 AM9/19/13
to quer...@googlegroups.com
Timo, I know JPA but what you mention, not exactly, do you have a clear example of this?.
Or someone who can give me an indication.
I am generating the statement to insert at runtime, if it do not be this with QueryDSL, I would like to know to keep it simple and do it with the typical JDBC.

br

On Wednesday, September 18, 2013 11:29:49 AM UTC-6, Norman Cash wrote:

Timo Westkämper

unread,
Sep 19, 2013, 11:50:20 AM9/19/13
to Querydsl on behalf of Norman Cash
Hi Norman.

On Thu, Sep 19, 2013 at 6:14 PM, Norman Cash via Querydsl <querydsl+noreply-APn2wQch2IJY-zk...@googlegroups.com> wrote:
Timo, I know JPA but what you mention, not exactly, do you have a clear example of this?.

What do you want to use? JPA or SQL? Querydsl works on both levels.
 
Or someone who can give me an indication.
I am generating the statement to insert at runtime, if it do not be this with QueryDSL, I would like to know to keep it simple and do it with the typical JDBC.

I don't understand. What do you want to do?

When using Querydsl with JPA you will do the inserts through the EntityManager, not via Querydsl.

Br,
Timo

br

On Wednesday, September 18, 2013 11:29:49 AM UTC-6, Norman Cash wrote:
I am developing an application using QueryDSL.
I would like to insert data into a table

I try to do this with SQLInsertDML. How can I do it?
Actually we are composing the text that contains insert instruction for example :

INSERT INTO TABLA1
(campo1,campo2)
values
(50,25);

I did not  find  an example over the internet.



--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Norman Cash

unread,
Sep 24, 2013, 1:03:53 PM9/24/13
to quer...@googlegroups.com
Timo, I have a application that generate sql dynamically.
My application is like this:

public Class Table {
        private String nameTable;
        private List<FieldTable> listField;
       
        public Table(String nameTable,List<FieldTable> listField) {
                   this.nameTable = nameTable;
                   this.listField     = listField;
        }
       
        //Getter and Setter of nameTable and listField
       ..........
}

public Class ServiceInsert {

         // Implementation of a Singleton
         ......

        public void insertTable(Table table,List<Object> values){
                    StringBuffer st = new StringBuffer("INSERT INTO " +  Table.getNameTable());
                    StringBuffer fieldSt = new StringBuffer();
                    StringBuffer valueSt = new StringBuffer();
                    for (Field field : Table.getListField){
                           fieldSt.append(field.getNameField + ",");
                           valueSt.append("?,");
                    }
                    st.append("(" + fieldSt.toString() + ") VALUES " + valueSt.toString);
                    //Connection JDBC for a Singletton
                    Connection con = ServiceConnection.getInstance().getConnection();
                     PreparedStatement ps = con.preparedStatement(st.toString);
                     //assign values
                    ....
                    ps.executeUpdate();
                    con.commit();
         }
}

@WebServlet("/send")
public class SendServlet extends HttpServlet {
    private static final long serialVersionUID = 7593256373572976563L;
    private static final Logger _logger = Logger.getLogger(EnvioServlet.class.getName());
   
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            _logger.info("start the sending ...");
            List<Field> fields = createField(request.getParameterMap());
                List<Object> Values=createValues(request.getParameterMap());
            Table newTable = new Table(request.getParameter("Table"),fields);
            ServiceInsert.getInstance().insertTable(newTable,values);
           
            _logger.info("End sending ...");
           
        } catch (Throwable e) {
            _logger.log(Level.SEVERE, e.getMessage());
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }
}

My application received the name of table , fields and the values.
I do not know how to create the QClass of something dinamic?. is it possible or
make Insert sql dinamic, only Knowing the name table, the name fields and the values.
How to do it?.


The requirements are well.
I already  made ​​changes to use with dynamic SQL queries QUeryDSL now have to do the same but with Insert so substitute the part of my code with preparedStatement using QueryDSL. This is my big question, as I do with QueryDSL.




On Wednesday, September 18, 2013 11:29:49 AM UTC-6, Norman Cash wrote:

Norman Cash

unread,
Sep 26, 2013, 11:17:44 AM9/26/13
to quer...@googlegroups.com
I would Know if It is possible?

br


On Wednesday, September 18, 2013 11:29:49 AM UTC-6, Norman Cash wrote:

Timo Westkämper

unread,
Sep 26, 2013, 11:20:52 AM9/26/13
to Querydsl on behalf of Norman Cash
Hi.

It's possible, but there is currently no easy way to do it for Querydsl.

Br,
Timo



--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Norman Cash

unread,
Sep 26, 2013, 11:36:39 AM9/26/13
to quer...@googlegroups.com
Any Idea Timo?.

This for me is important.

Br


On Wednesday, September 18, 2013 11:29:49 AM UTC-6, Norman Cash wrote:

Timo Westkämper

unread,
Sep 26, 2013, 3:50:44 PM9/26/13
to Querydsl on behalf of Norman Cash
Hi.

Querydsl is not really that useful as a SQL builder library if you need to create all the expressions from scratch. This works quite well if you have a domain, but for SQL it is a bit verbose.

If you want to be a pioneer in this area then try something like this

RelationalPath<Object> table = new RelationalPathBase<Object>(Object.class, "tableAlias", "schemaName", "schemaAlias");
PathBuilder<Object> pathBuilder = new PathBuilder<Object>(Object.class, table.getMetadata());

SQLInsertClause insert = new SQLInsertClause(connection, templates, table);
for (Map.Entry<String,Object> entry : ...) {
    insert.set(pathBuilder.get(entry.getKey()), entry.getValue());
}
insert.execute();

There isn't yet a subclass of RelationalPathBase that would provide dynamic property creation like PathBuilder does, that's why you need to use two classes to reference the main entity.

Most of the rest should be covered in the Querydsl reference docs http://www.querydsl.com/static/querydsl/3.2.3/reference/html/

Br,
Timo


--
You received this message because you are subscribed to the Google Groups "Querydsl" group.
To unsubscribe from this group and stop receiving emails from it, send an email to querydsl+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Norman Cash

unread,
Sep 26, 2013, 6:06:19 PM9/26/13
to quer...@googlegroups.com
Thanks you Timo.


On Wednesday, September 18, 2013 11:29:49 AM UTC-6, Norman Cash wrote:
Reply all
Reply to author
Forward
0 new messages