How to select all the data of two columns from a table using JPA query method
Create a result class first:
package com.example;
public class ResultClass{
private Long id;
private String name;
public ResultCalss(Long id, String name){
// set
}
}
and then use a custom @Query:
@Query("select new com.example.ResultClass(e.id, e.name) from MyEntity e")
public List<ResultClass> findIdsAndNames();
java- how to return multiple columns in JPA for email?
Try replacing the query call with:
List<Emailqueue> res = em.createQuery("SELECT e from Emailqueue e WHERE e.status='Pending'",
Emailqueue.class).getResultList();
This will return a list of matching instances as Java objects (entities), which you can easily work with by calling getters/setters, for example (using the sendEmail
method from your question):
for (Emailqueue eq : res) {
sendStatus = sendEmail(eq.getRecipient(), eq.getSubject(), eq.getContent(), eq.getId(), eq.getUsername());
}
Or, better yet, change the signature of sendEmail
to sendEmail(Emailqueue eq)
and call the getters in the method.
use jpa nativequery multiple columns in object list array
I would try to restructure the query as follows:
SELECT * FROM TABLE A
WHERE (A.COL1 = 'val1' and A.COL2 = 'val2')
OR (A.COL1 = 'val3' and A.COL2 = 'val4')
This would allow the query to be constructed as follows:
List<String[]> queryList = new ArrayList<>();
String[] arr = {"val1", "val2"};
String[] arr = {"val3", "val4"};
queryList.add(arr);
String sql = "SELECT * FROM TABLE A "; //dont forget space at end
if (!queryList.isEmpty()){
sql = sql + "WHERE "; //dont forget space at end
for (String[] queryParam : queryList ){
sql = sql + " (A.COL1 = '"+ queryParam[0] + "' and A.COL2 = '" + queryParam[1] + "') OR "; //dont forget space at end and simple colons for param
}
//finally remove the last OR.
Integer indexLastOR = sql.lastIndexOf("OR");
sql = sql.substring(0, indexLastOR);
}
Query query = entityManager.createNativeQuery(sql);
This will also allow the query to be implemented without being native, which is advisable to maintain the JPA philosophy.
SQL JPA - Multiple columns as primary key
You need to have a class for your composite key:
public class CompositeKey implements Serializable {
private int column1;
private int column2;
private int column3;
}
and then in your entity class use the @IdClass
annotation:
@Entity
@IdClass(CompositeKey.class)
public class EntityExample {
@Id
private int column1;
@Id
private int column2;
@Id
private int column3;
...
...
}
I think this should work. Hope it helps, cheers!
Yea and there is the other solution, the one that @jklee mentioned, both work, it's a matter of preference.
Related Topics
How to Trim White Space from All Elements in Array
Regular Expression for Uppercase Letters in a String
@Responsebody , Responseentity Spring Return Object as Json
How to Find Distinct Rows With Field in List Using JPA and Spring
How to Click an Li Inside a Ul Using Selenium Webdriver
How to Skip the First Line of a CSV in Java
A Method to Check If a Collection or Map Is Empty or Null
How to Disable Log4J Logging from Java Code
How to Connect Java Backend With Html/Css Frontend
Spring @Value Annotation Always Evaluating as Null
How to Get the First Date and Last Date of the Previous Month (Java)
How to Make the Icon Background of an Android App Transparent
How to Remove Padding Around Buttons in Android
Fixing Apache V9.0 Server Tomcat V9.0 Server At Localhost Failed to Start Error
Replacing Json String With Another String in Java
Sqlite_Error - SQL Error or Missing Database (Near "Autoincrement": Syntax Error)