inner-join query in Hibernate - sql

I want to run a query with Hibernate, but I don't know what I'm donig wrong that I get errors.
I tried following
public List<String> findCourseForStudent(String pnr) {
factory = new Configuration().configure().buildSessionFactory();
Session session = factory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
String sql = "select Course.name from Course inner join CourseMaterial "
+ "on CourseMaterial.course_id = Course.id inner join CourseParticipantship "
+ "on CourseMaterial.id = CourseParticipantship.courseMaterial_id inner join Student "
+ "on Student.id=CourseParticipantship.student_id where Student.personalNumber='" + pnr + "'";
SQLQuery query = session.createSQLQuery(sql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
query.setParameter("pnr", pnr);
List data = query.list();
tx.commit();
return data;
} catch(HibernateException e) {
if(tx != null) tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return null;
}
but I get the following error:
net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: DefaultConfigurationSource [ ehcache.xml or ehcache-failsafe.xml ]
I've searched for the error and tried some of the solutions, but none of them worked for me, so I tried to use HQL instead of using sql directly, but still no success :(
I don't actually know how to use INNER_JOIN in DetachedCriteria otherwise I think using DetachedCriteria I won't get any errors because I tried to use it for simpler queries and it worked fine.
Following is an example for a simple query that works fine, but I don't know how to wirte it for innerjoin queries
public List<Student> findForCourse(Integer integer) {
DetachedCriteria criteria = DetachedCriteria.forClass(Student.class);
criteria.add(Restrictions.eq("course.id", integer));
criteria.add(Restrictions.ne("active", Boolean.FALSE));
return getHibernateTemplate().findByCriteria(criteria);
}

Finally, I found out how to write my query in hibernate
public List<String> findCourseForStudent(String pnr) {
return jdbcTemplate.query(
"select Course.name from Course inner join CourseMaterial "
+ "on CourseMaterial.course_id = Course.id inner join CourseParticipantship "
+ "on CourseMaterial.id = CourseParticipantship.courseMaterial_id inner join thd.dbo.Student "
+ "on Student.id=CourseParticipantship.student_id where Student.personalNumber= ?", new Object[] { pnr }, new RowMapper() {
public Object mapRow(ResultSet rs, int row) throws SQLException {
return rs;
}
});
}

Related

How to fail the testng script if while(resultset.next()) returns false?

Sometimes,
dbResultSetSalary.next()
doesn't return any value for my query, which is false. In that case, how to fail the testng script?
Below is my code :-
import java.sql.ResultSet;
import org.testng.annotations.Test;
public class Test21 {
static ResultSet dbResultSetEmployeeID;
static ResultSet dbResultSetSalary;
#Test
public void dbTest() throws Exception {
DataBaseUtil conn = new DataBaseUtil();
conn.EstablishConnection("ORACLE");
dbResultSetEmployeeID = conn.executeQuery("SELECT * FROM EMPLOYEE WHERE EMPLOYEE_NAME = 'StackOverFlow'");
while(dbResultSetEmployeeID.next()) {
String id = dbResultSetEmployeeID.getString("EMP_ID");
dbResultSetSalary = conn.executeQuery("SELECT * FROM SALARY WHERE EMP_ID = '"+id+"'");
while(dbResultSetSalary.next()) {
String val = dbResultSetSalary.getString("VAL");
System.out.println(val);
assertEquals("23400", val)
}
}
}
}
I am new to db connection using oracle. need some insight on this.
I suggest maintaining a single Boolean variable which keeps track of whether or not the test passed. It would be initialized to false, and only would be set true upon completion of a successful query with the correct value.
#Test
public void dbTest() throws Exception {
boolean success = false; // added here
DataBaseUtil conn = new DataBaseUtil();
conn.EstablishConnection("ORACLE");
String sqlName = "SELECT * FROM EMPLOYEE WHERE EMPLOYEE_NAME = 'StackOverFlow'";
dbResultSetEmployeeID = conn.executeQuery(sqlName);
while (dbResultSetEmployeeID.next()) {
String id = dbResultSetEmployeeID.getString("EMP_ID");
String sqlSalary = "SELECT * FROM SALARY WHERE EMP_ID = '" + id + "'";
dbResultSetSalary = conn.executeQuery(sqlSalary);
if (dbResultSetSalary.next()) {
String val = dbResultSetSalary.getString("VAL");
System.out.println(val);
assertEquals("23400", val);
success = true;
}
}
// JUnit will interpret an exception as a test failure
if (!success) {
throw new Exception("Test failed.");
}
}
Notes:
You are actually running an integration test here, not a unit test, because the result of your test does not just depend on a unit of code functionality, it also depends on whether your actual database be working.
One of your SQL queries is built using string concatenation. This is generally prone to SQL injection, but since you are controlling what values go in there, there is in theory no risk of SQL injection.
You can use hasNext():
if(!dbResultSetSalary.hasNext())
throw new MyException("You are lying")
and if you want to check the value, then:
if(val==null or whatever)
throw new MyException("You are lying for the second time")

Using NHibernate with SAP Advantage Database Server 12

I'm currently trying to make NHibernate work with SAP's Advantage Database Server 12. Unfortunately, there is not much information around this database server in the internet, but in this case, I have to use it to access an existing database.
So I tried to set up a simple test project first which adds "Students" in a Student table, following this: https://www.tutorialspoint.com/nhibernate/nhibernate_basic_orm.htm
As for the ADS, I found a driver and dialect here: https://groups.google.com/forum/#!topic/nhibernate-development/s7HLs-aEXDk
I've set up a table and this code:
private void button1_Click(object sender, EventArgs e)
{
var cfg = new Configuration();
cfg.DataBaseIntegration(x =>
{
x.ConnectionString = "data source=C:\\Users\\xxx\\Downloads;ServerType=local; TableType=ADT; Shared=True; LockMode=COMPATIBLE";
x.Driver<AdvantageDatabaseServerDriver>();
x.Dialect<AdvantageDatabaseServer8Dialect>();
x.LogSqlInConsole = true;
});
cfg.AddAssembly(Assembly.GetExecutingAssembly());
var sefact = cfg.BuildSessionFactory();
using (var session = sefact.OpenSession())
{
using (var tx = session.BeginTransaction())
{
//perform database logic
tx.Commit();
}
Console.ReadLine();
}
using (var session = sefact.OpenSession())
{
using (var tx = session.BeginTransaction())
{
var student1 = new Students
{
ID = 1,
FirstMidName = "Allan",
LastName = "Bommer"
};
var student2 = new Students
{
ID = 2,
FirstMidName = "Jerry",
LastName = "Lewis"
};
session.Save(student1);
session.Save(student2);
tx.Commit();
}
Console.ReadLine();
}
}
But it keeps throwing this exception at the first write attempt:
NHibernate: INSERT INTO C:\Users\ad\Downloads\REFERENCE\Students.adt (LastName, FirstMidName) VALUES (?, ?); select NEWIDSTRING() from system.iota;p0 = 'Glenn' [Type: String (-1:0:0)], p1 = 'Allan' [Type: String (-1:0:0)]
Exception thrown: 'Advantage.Data.Provider.AdsException' in NHibernate.dll
Is there anything wrong with the dialect maybe? I really appreciate any help on this, thank you!
From the comments the problem is this:
NHibernate with the grammar you are using emits this code:
INSERT INTO
C:\Users\ad\Downloads\REFERENCE\Students.adt
(
LastName
, FirstMidName
)
VALUES
(
?
, ?
);
select NEWIDSTRING() from system.iota;
Apparently the ASP.NET driver for ADS can not handle this form of parameter passing correctly.
The knowledge base item you linked to suggest to change the SQL to use named parameters instead:
INSERT INTO
C:\Users\ad\Downloads\REFERENCE\Students.adt
(
LastName
, FirstMidName
)
VALUES
(
:p0
, :p1
);
select NEWIDSTRING() from system.iota;
I don't know enough of NHibernate to tell you how this can be achieved.

Combining two different jTables and adding button into jTable

I try to do for forum using java swing. Here are my codes for table :
public void SetUpJTable() {
DefaultTableModel tableModel = (DefaultTableModel) jTable.getModel();
String[] data = new String[4];
db.setUp("IT Innovation Project");
String sql = "Select topic_title,topic_description,topic_by from forumTopics WHERE topic_id = "
+ topicId + "";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
data[0] = resultSet.getString("topic_title");
data[1] = resultSet.getString("topic_description");
data[2] = resultSet.getString("topic_by");
tableModel.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
I set up this table to retrieve the topic details which user select certain thread from main page. And I set up another table to store for the replies by users. Here is it :
public void SetUpJTableComment() {
DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
.getModel();
String[] data = new String[3];
db.setUp("IT Innovation Project");
String sql = "Select reply_content,reply_by from forumReplies WHERE reply_topic = "
+ topicId + "";
ResultSet resultSet = null;
resultSet = db.readRequest(sql);
try {
while (resultSet.next()) {
data[0] = resultSet.getString("reply_content");
data[1] = resultSet.getString("reply_by");
tableModel1.addRow(data);
}
resultSet.close();
} catch (Exception e) {
System.out.println(e);
}
}
And this is how I set up the table :
private JTable getJTableComment() {
String header[] = { "Comment", "Reply By" };
if (jTableComment == null) {
jTableComment = new JTable() {
public boolean isCellEditable(int nRow, int nCol) {
return false;
}
};
}
DefaultTableModel tableModel1 = (DefaultTableModel) jTableComment
.getModel();
tableModel1.setColumnIdentifiers(header);
jTableComment.getColumnModel().getColumn(0).setMinWidth(700);
jTableComment.getColumnModel().getColumn(0).setMaxWidth(800);
jTableComment.getColumnModel().getColumn(1).setMinWidth(97);
jTableComment.getColumnModel().getColumn(1).setMaxWidth(100);
jTableComment.getTableHeader().setFont(
new Font("Dialog", Font.PLAIN, 20));
jTableComment.getTableHeader().setForeground(Color.white);
jTableComment.getTableHeader().setBackground(new Color(102, 102, 102));
jTableComment.setRowHeight(50);
jTableComment.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
jTableComment.setFont(new Font("Dialog", Font.PLAIN, 18));
return jTableComment;
}
It works perfectly with two separating tables. I wonder if there is some way to combine both of these tables into one table? And how can I customize the table to make it look less-liked a table because my current one is just .. solid-table and my teacher asked me to improve it but I have no idea to do so. And I tried to add button into the table but I realized that I cannot add it from the try statement because that is is retrieve data from database directly. Any guides? Thanks in advance.
You can use SQL join construct and have one table with more columns:
select topic_title,topic_description,topic_by,
reply_content,reply_by
from forumTopics join forumReplies
on (forumTopics.topic_id=forumReplies.topic_id) WHERE topic_id = 1234
then build the model from the five column result set as you are already doing.
But surely if there is more than one reply to a forum topic, the topic part will be repeated in the table.
To make a table not to look like a table, try JTreeTable from Swing Labs maybe, it allows to have tree-like subsections, exactly that is required. It is not part of the system library however, you will need to download it. Some source code on how to just JTreeTable can be found here.
On how JTreeTable looks is Swing Labs, you can see in they web-startable demo. It also shows the code sample automatically.

Exception in creating a delete query in EntityManager

Here is the method:
public void deleteVotesByReplyID(long replyId) {
EntityManager em = getEntityManager();
try {
int re = em.createQuery("delete object(o)
from Vote as o
where o.memberReply.id = '"+replyId+"'"
).executeUpdate();
} finally {
em.close();
}
}
What's wrong in above query? (used jpa 1.0)
Perhaps due to the fact that a delete query starts
DELETE FROM entity_name [[AS] identification_variable] [WHERE <filter>]
Your query could have been written as
"DELETE from Vote o where o.memberReply.id = 'someId'"
Also DELETE queries can only be executed within an active transaction. You will want
em.getTransaction().begin();
query.executeUpdate();
em.getTransaction().commit();
and also to
catch (Exception e) {em.getTransaction().rollback();}

get next sequence value from database using hibernate

I have an entity that has an NON-ID field that must be set from a sequence.
Currently, I fetch for the first value of the sequence, store it on the client's side, and compute from that value.
However, I'm looking for a "better" way of doing this. I have implemented a way to fetch the next sequence value:
public Long getNextKey()
{
Query query = session.createSQLQuery( "select nextval('mySequence')" );
Long key = ((BigInteger) query.uniqueResult()).longValue();
return key;
}
However, this way reduces the performance significantly (creation of ~5000 objects gets slowed down by a factor of 3 - from 5740ms to 13648ms ).
I have tried to add a "fake" entity:
#Entity
#SequenceGenerator(name = "sequence", sequenceName = "mySequence")
public class SequenceFetcher
{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")
private long id;
public long getId() {
return id;
}
}
However this approach didn't work either (all the Ids returned were 0).
Can someone advise me how to fetch the next sequence value using Hibernate efficiently?
Edit: Upon investigation, I have discovered that calling Query query = session.createSQLQuery( "select nextval('mySequence')" ); is by far more inefficient than using the #GeneratedValue- because of Hibernate somehow manages to reduce the number of fetches when accessing the sequence described by #GeneratedValue.
For example, when I create 70,000 entities, (thus with 70,000 primary keys fetched from the same sequence), I get everything I need.
HOWEVER , Hibernate only issues 1404 select nextval ('local_key_sequence') commands. NOTE: On the database side, the caching is set to 1.
If I try to fetch all the data manually, it will take me 70,000 selects, thus a huge difference in performance. Does anyone know the internal functioning of Hibernate, and how to reproduce it manually?
You can use Hibernate Dialect API for Database independence as follow
class SequenceValueGetter {
private SessionFactory sessionFactory;
// For Hibernate 3
public Long getId(final String sequenceName) {
final List<Long> ids = new ArrayList<Long>(1);
sessionFactory.getCurrentSession().doWork(new Work() {
public void execute(Connection connection) throws SQLException {
DialectResolver dialectResolver = new StandardDialectResolver();
Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement( dialect.getSequenceNextValString(sequenceName));
resultSet = preparedStatement.executeQuery();
resultSet.next();
ids.add(resultSet.getLong(1));
}catch (SQLException e) {
throw e;
} finally {
if(preparedStatement != null) {
preparedStatement.close();
}
if(resultSet != null) {
resultSet.close();
}
}
}
});
return ids.get(0);
}
// For Hibernate 4
public Long getID(final String sequenceName) {
ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
#Override
public Long execute(Connection connection) throws SQLException {
DialectResolver dialectResolver = new StandardDialectResolver();
Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
preparedStatement = connection.prepareStatement( dialect.getSequenceNextValString(sequenceName));
resultSet = preparedStatement.executeQuery();
resultSet.next();
return resultSet.getLong(1);
}catch (SQLException e) {
throw e;
} finally {
if(preparedStatement != null) {
preparedStatement.close();
}
if(resultSet != null) {
resultSet.close();
}
}
}
};
Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork);
return maxRecord;
}
}
Here is what worked for me (specific to Oracle, but using scalar seems to be the key)
Long getNext() {
Query query =
session.createSQLQuery("select MYSEQ.nextval as num from dual")
.addScalar("num", StandardBasicTypes.BIG_INTEGER);
return ((BigInteger) query.uniqueResult()).longValue();
}
Thanks to the posters here: springsource_forum
I found the solution:
public class DefaultPostgresKeyServer
{
private Session session;
private Iterator<BigInteger> iter;
private long batchSize;
public DefaultPostgresKeyServer (Session sess, long batchFetchSize)
{
this.session=sess;
batchSize = batchFetchSize;
iter = Collections.<BigInteger>emptyList().iterator();
}
#SuppressWarnings("unchecked")
public Long getNextKey()
{
if ( ! iter.hasNext() )
{
Query query = session.createSQLQuery( "SELECT nextval( 'mySchema.mySequence' ) FROM generate_series( 1, " + batchSize + " )" );
iter = (Iterator<BigInteger>) query.list().iterator();
}
return iter.next().longValue() ;
}
}
If you are using Oracle, consider specifying cache size for the sequence. If you are routinely create objects in batches of 5K, you can just set it to a 1000 or 5000. We did it for the sequence used for the surrogate primary key and were amazed that execution times for an ETL process hand-written in Java dropped in half.
I could not paste formatted code into comment. Here's the sequence DDL:
create sequence seq_mytable_sid
minvalue 1
maxvalue 999999999999999999999999999
increment by 1
start with 1
cache 1000
order
nocycle;
To get the new id, all you have to do is flush the entity manager. See getNext() method below:
#Entity
#SequenceGenerator(name = "sequence", sequenceName = "mySequence")
public class SequenceFetcher
{
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence")
private long id;
public long getId() {
return id;
}
public static long getNext(EntityManager em) {
SequenceFetcher sf = new SequenceFetcher();
em.persist(sf);
em.flush();
return sf.getId();
}
}
POSTGRESQL
String psqlAutoincrementQuery = "SELECT NEXTVAL(CONCAT(:psqlTableName, '_id_seq')) as id";
Long psqlAutoincrement = (Long) YOUR_SESSION_OBJ.createSQLQuery(psqlAutoincrementQuery)
.addScalar("id", Hibernate.LONG)
.setParameter("psqlTableName", psqlTableName)
.uniqueResult();
MYSQL
String mysqlAutoincrementQuery = "SELECT AUTO_INCREMENT as id FROM information_schema.tables WHERE table_name = :mysqlTableName AND table_schema = DATABASE()";
Long mysqlAutoincrement = (Long) YOUR_SESSION_OBJ.createSQLQuery(mysqlAutoincrementQuery)
.addScalar("id", Hibernate.LONG)
.setParameter("mysqlTableName", mysqlTableName)
.uniqueResult();
Interesting it works for you. When I tried your solution an error came up, saying that "Type mismatch: cannot convert from SQLQuery to Query". --> Therefore my solution looks like:
SQLQuery query = session.createSQLQuery("select nextval('SEQUENCE_NAME')");
Long nextValue = ((BigInteger)query.uniqueResult()).longValue();
With that solution I didn't run into performance problems.
And don't forget to reset your value, if you just wanted to know for information purposes.
--nextValue;
query = session.createSQLQuery("select setval('SEQUENCE_NAME'," + nextValue + ")");
Spring 5 has some builtin helper classes for that:
org/springframework/jdbc/support/incrementer
Here is the way I do it:
#Entity
public class ServerInstanceSeq
{
#Id //mysql bigint(20)
#SequenceGenerator(name="ServerInstanceIdSeqName", sequenceName="ServerInstanceIdSeq", allocationSize=20)
#GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ServerInstanceIdSeqName")
public Long id;
}
ServerInstanceSeq sis = new ServerInstanceSeq();
session.beginTransaction();
session.save(sis);
session.getTransaction().commit();
System.out.println("sis.id after save: "+sis.id);
Your idea with the SequenceGenerator fake entity is good.
#Id
#GenericGenerator(name = "my_seq", strategy = "sequence", parameters = {
#org.hibernate.annotations.Parameter(name = "sequence_name", value = "MY_CUSTOM_NAMED_SQN"),
})
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
It is important to use the parameter with the key name "sequence_name". Run a debugging session on the hibernate class SequenceStyleGenerator, the configure(...) method at the line final QualifiedName sequenceName = determineSequenceName( params, dialect, jdbcEnvironment ); to see more details about how the sequence name is computed by Hibernate. There are some defaults in there you could also use.
After the fake entity, I created a CrudRepository:
public interface SequenceRepository extends CrudRepository<SequenceGenerator, Long> {}
In the Junit, I call the save method of the SequenceRepository.
SequenceGenerator sequenceObject = new SequenceGenerator();
SequenceGenerator result = sequenceRepository.save(sequenceObject);
If there is a better way to do this (maybe support for a generator on any type of field instead of just Id), I would be more than happy to use it instead of this "trick".