//java code如下
TypedQuery<?> outputSql = entityManager.createQuery(query);
org.eclipse.persistence.sessions.Session session = communityEm.unwrap(org.eclipse.persistence.jpa.JpaEntityManager.class).getActiveSession();
org.eclipse.persistence.queries.DatabaseQuery databaseQuery = outputSql.unwrap(org.eclipse.persistence.jpa.JpaQuery.class).getDatabaseQuery();
databaseQuery.prepareCall(session, new org.eclipse.persistence.sessions.DatabaseRecord());
System.out.println("databaseQuery.getSQLString()"+databaseQuery.getSQLString());
分类:Java
Idea工具常用快捷键
- 优化 import java classes
//消除不用的 import classes
Shift+Command + A , then Enter
- Find java file
Control + Shift + O
- Find keyword in projects
Control + Shift + F
用java JPA进行多表join
例如我们有如下多表查询
select A.id, A.name, B.name2, C.name2 from A,B,C where A.id=B.bid and B.name3 = C.id
我们用Join连接 JPA tables
// Root rootpath Entity A,B,C in B has relasionship B.name3 = C Join b_path = rootpath.join("id");
Join c_path = b_path.join("name3");
Java 小知识
1. Get client timezone
Calendar.getInstance().getTimeZone()
2. JPA sql 输出
//queryType:TypedQuery
Java 脚本连接hana 数据库
请见如下code:获取hana kernel信息 和 license 信息
请 google ‘ngdbc jar’ 或直接下载附件zip包,解压后引用jar包即可 连接hana驱动jar
import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; static void query_info(String ip, String port, String tename, String user, String password) { String perline = ""; Connection connection = null; String hostname = null; String lisdate = null; String liedate = null; String version = null; String kernel = null; try { Class.forName("com.sap.db.jdbc.Driver"); String url = "jdbc:sap://" + ip + ":" + port + "/" + tename + ""; connection = java.sql.DriverManager.getConnection(url, user, password); System.out.println("Connection done"); } catch (Exception e) { System.err.println(ip + " Connection failed!" + e.getMessage()); } if (connection != null) { try { System.out.println(ip + " Connection to HANA successful!"); // query license start/end date Statement stmt = connection.createStatement(); ResultSet resultSet = stmt.executeQuery("select * from SYS.M_LICENSE "); resultSet.next(); lisdate = resultSet.getString("START_DATE"); liedate = resultSet.getString("EXPIRATION_DATE"); System.out.println(lisdate); System.out.println(liedate); // query version info stmt = connection.createStatement(); resultSet = stmt.executeQuery("select value from SYS.M_SYSTEM_OVERVIEW where name='Version'"); resultSet.next(); version = resultSet.getString("value"); // query kernel info stmt = connection.createStatement(); resultSet = stmt .executeQuery("select host,value from SYS.M_HOST_INFORMATION where key='os_kernel_version'"); resultSet.next(); kernel = resultSet.getString("value"); hostname = resultSet.getString("host"); System.out.println(version); System.out.println(kernel); System.out.println(hostname); resultSet.close(); stmt.close(); connection.close(); } catch (SQLException e) { System.err.println("Query failed!" + e.getMessage()); } } }