JDBCTemplate RowMapper for Inner Join - sql-server

Server inform error
HTTP Status 500 - Request processing failed; nested exception is
org.springframework.dao.TransientDataAccessResourceException:
StatementCallback; SQL [SELECT * FROM Accounts INNER JOIN Comment ON
Comment.Account_ID=Accounts.Account_ID WHERE Comment.Place_ID=3]; The
column name Accounts.Account_ID is not valid.; nested exception is
com.microsoft.sqlserver.jdbc.SQLServerException: The column name
Accounts.Account_ID is not valid.
This is my code in DAO
public List<LoadCommentDTO> loadComment(int placeId) {
String sql = "SELECT * FROM Accounts INNER JOIN Comment ON Comment.Account_ID=Accounts.Account_ID WHERE Comment.Place_ID="+ placeId ;
RowMapper<LoadCommentDTO> rowMapper = new CommentRowMapper();
List<LoadCommentDTO> listComment = jdbcTemplate.query(sql, rowMapper);
return listComment;
}
protected class CommentRowMapper implements RowMapper<LoadCommentDTO> {
#Override
public LoadCommentDTO mapRow(ResultSet rs, int rowNum) throws SQLException {
LoadCommentDTO loadCommentDTO = new LoadCommentDTO();
CommentDTO commentDTO = new CommentDTO();
AccountDTO accountDTO = new AccountDTO();
accountDTO.setAccountID(rs.getInt("Accounts.Account_ID"));
accountDTO.setAvatar_path(rs.getString("Accounts.Avatar_path"));
accountDTO.setFirstname(rs.getString("Accounts.Firstname"));
accountDTO.setLastname(rs.getString("Accounts.Lastname"));
commentDTO.setAccount_Id(rs.getInt("Comment.Account_ID"));
commentDTO.setDescription(rs.getString("CommentDescription"));
commentDTO.setPlace_ID(rs.getInt("CommentPlace_ID"));
commentDTO.setC_Date(rs.getString("CommentC_Date"));
loadCommentDTO.setAccountDTO(accountDTO);
loadCommentDTO.setCommentDTO(commentDTO);
return loadCommentDTO;
}
}

Related

Writing a test class for Opportunity line items and I keep getting a pricebookentry error

I keep getting the following error when I write a test class for opportunity line items.
System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, field integrity exception: PricebookEntryId (pricebook entry is in a different pricebook than the one assigned to the opportunity): [PricebookEntryId]
Here is my code.
#isTest
public class PreventLineItemDeletionTestClass {
static testmethod void PreventLineItemDeletionTestClass(){
Id pricebookId = Test.getStandardPricebookId();
// insert product
Product2 p = new Product2();
p.Name = ' Test Product ';
p.Description='Test Product Entry For Product';
p.productCode = 'SFDCPanther-123';
p.isActive = true;
insert p;
// insert pricebook entry for the product
PricebookEntry pbEntry = new PricebookEntry(
Pricebook2Id = pricebookId ,
Product2Id = p.Id,
UnitPrice = 100.00,
IsActive = true);
insert pbEntry;
// Create Opportunity
Opportunity opp = new Opportunity();
opp.Name = 'Opp for Test Account New';
opp.CloseDate= System.Today();
opp.StageName='Negotiation';
opp.Pricebook2Id = pricebookId;
insert opp;
system.debug('opp.pricebook2Id!!!!'+opp.pricebook2Id+'pbEntry.Pricebook2Id !!!'+pbEntry.Pricebook2Id);
// Add product and Pricebook to the particular opportunity using OpportunityLineItem
OpportunityLineItem ol = new OpportunityLineItem();
ol.opportunityid = opp.id;
ol.quantity = 4;
ol.TotalPrice = ol.quantity * pbEntry.UnitPrice ;
ol.Product2Id = p.id;
ol.PricebookEntryId = pbEntry.Id;
ol.Fulfillment__c = 2;
insert ol;
}
}
Could someone please let me know what I am doing wrong.
Best,
Mike

Updlock, Holdlock, Rowlock usage? Pessimistic Locking in SQL Server

I have some question about Pessimistic Locking in SQL Server? Here are my classes and test scenario;
Entity class:
#Data
#Entity(name = "mapping")
#Table(
uniqueConstraints =
#UniqueConstraint(
name = "UQ_MappingEntity",
columnNames = {
Constants.DATA_TYPE_VALUE,
Constants.DATA_TYPE_NAMESPACE_INDEX,
Constants.TENANT_ID,
Constants.ASSET_TYPE_NAME
}
)
)
public class MappingEntity {
#Id
#GeneratedValue(generator = "uuid")
#GenericGenerator(name = "uuid", strategy = "uuid2")
private String id;
#Column(name = Constants.DATA_TYPE_VALUE)
private long dataTypeValue;
#Column(name = Constants.DATA_TYPE_NAMESPACE_INDEX)
private int dataTypeNamespaceIndex;
#Column(name = Constants.ASSET_TYPE_NAME)
private String assetTypeName;
#Column(name = Constants.TENANT_ID)
private String tenantId;
}
Repository class:
public interface MappingRepository extends JpaRepository<MappingEntity, String> {
#Lock(LockModeType.PESSIMISTIC_WRITE)
MappingEntity findMappingEntityWithLockByTenantIdAndAssetTypeName(
String tenantId, String assetTypeName);
}
Service code block:
#Transactional
public void deleteAspectType(String tenantId, String aspectTypeId) {
MappingEntity mappingEntity = mappingRepository.findMappingEntityWithLockByTenantIdAndAssetTypeName(tenantId, assetTypeName);
mappingRepository.delete(mappingEntity);
}
When I enable the hibernate logs. I see select query below.
select
mappingent0_.id as id1_1_,
mappingent0_.asset_type_name as asset_ty2_1_,
mappingent0_.data_type_namespace_index as data_typ3_1_,
mappingent0_.data_type_value as data_typ4_1_,
mappingent0_.tenant_id as tenant_i5_1_
from
mapping mappingent0_ with (updlock,
holdlock,
rowlock)
where
mappingent0_.tenant_id=?
and mappingent0_.asset_type_name=?
I have sent two delete request at the same time with same tenant_id but different asset_type_name;
Transaction-1: tenant_id = "testtenant", asset_type_name = "testname1"
Transaction-2: tenant_id = "testtenant", asset_type_name = "testname2"
Transaction-1 run select query and gets results, When Transaction-2 run select query it blocks. After Transaction-1 deletes and finishes the transaction, Transaction-2 get results and deletes.
I have two question;
What are the (updlock, holdlock, rowlock) use for? When I use these three same time, how does effect my query and transaction?
Why did Transaction-2 block when it run the query? Because Both transaction selected different rows.

Second Order SQL Injection - PreparedStatement

The following section of my code is raising concern in "Second-Order SQL Injection".
private String function1 (String var1) {
String sql = "SELECT field1 FROM table1 WHERE field2 = ?";
PreparedStatement ps = null;
ResultSet resultSet = null;
String result = "";
try{
ps = conn.prepareStatement(sql);
ps.setString(1, var1);
resultSet = ps.executeQuery();
if(resultSet.next()){
result = rs.getString("fldDesc");
}
}catch(SQLException e){
e.printStackTrace();
}
}
However, as I know the preparedStatement should be safe against the 2nd Order SQL Injection, due to the separation of query and data.
Can I know why does it would raise a concern against it?

How to Deal JPA / JPQL with SQL Server Keyword

My Entity class
#Entity
class MasterStccycode{
private static final long serialVersionUID = 1L;
#Id
#Basic(optional = false)
#NotNull
#Size(min = 1, max = 3)
#Column(name = "CODE")
private String code;
#Size(max = 100)
#Column(name = "DESC")
private String desc;
}
my JPA Query
SELECT t.code, t.desc FROM MasterStccycode t
then I have this following exception
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'DESC'.
Error Code: 156
Call: SELECT CODE, DESC FROM master_stccycode
Query: ReportQuery(referenceClass=MasterStccycode sql="SELECT CODE, DESC FROM master_stccycode")
I know the solution is to wrap the DESC keyword with [] into [DESC] but how can I do this on JPA QL?
DESC is a reserved word on most databases. You should rename the field.
You could also quote the field, but just renaming it would be best.
#Column(name = "\"DESC\"")

jpa doesn't see table in db

When I execute
#NamedQuery(name = "GroupsHasStudent.findByGroupsidGroups", query = "SELECT g FROM GroupsHasStudent g WHERE g.groupsHasStudentPK.groupsidGroups = :groupsidGroups"),
of GroupsHasStudent entity, the result is generated correct. But another NativeQuery
getEntityManager().
createNativeQuery(
"SELECT d.idDiscipline, d.name, gy.idGroupsYear, gy.year, g.idGroups "
+ "FROM Discipline d, Groupsyear gy, Groups g, GroupsHasStudent ghs "
+ "WHERE ghs.groupsHasStudentPK.groupsidGroups=2").
getResultList();
throws the Exception
Internal Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:
Table 'mydb.groupshasstudent' doesn't exist
Actually I do don't have such table in db, but the correct name is *groups_has_student* which is specified in #Table:
(My entity:)
#Entity
#Table(name = "groups_has_student")
#XmlRootElement
#NamedQueries({
#NamedQuery(name = "GroupsHasStudent.findAll", query = "SELECT g FROM GroupsHasStudent g"),
#NamedQuery(name = "GroupsHasStudent.findByGroupsidGroups", query = "SELECT g FROM GroupsHasStudent g WHERE g.groupsHasStudentPK.groupsidGroups = :groupsidGroups"),
#NamedQuery(name = "GroupsHasStudent.findByStudentUserslogin", query = "SELECT g FROM GroupsHasStudent g WHERE g.groupsHasStudentPK.studentUserslogin = :studentUserslogin")})
public class GroupsHasStudent implements Serializable {
private static final long serialVersionUID = 1L;
#EmbeddedId
protected GroupsHasStudentPK groupsHasStudentPK;
public GroupsHasStudent() {
}
And even when I rename table in mysql to groupshasstudent there is another Exception
Unknown column 'ghs.groupsHasStudentPK.studentUserslogin' in 'where clause'
І це сумно..
It's not a native (SQL) query, it's a JPQL query, therefore you should use createQuery() instead of createNativeQuery().

Resources