Saturday, 21 February 2015

Hibernate Issue : Unable To Insert If Column Named Is Keyword, Such As DESC

Problem

A table named “category” in MySQL database, contains a “DESC” keyword as column name.
CREATE TABLE `category` (
  `CATEGORY_ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `NAME` VARCHAR(10) NOT NULL,
  `DESC` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`CATEGORY_ID`) USING BTREE
);
Hibernate XML mapping file
<hibernate-mapping>
    <class name="com.mkyong.stock.Category" table="category" catalog="mkyongdb">
        ...
        <property name="desc" type="string">
            <column name="DESC" not-null="true" />
        </property>
       ...
    </class>
</hibernate-mapping>
Or Hibernate annotation
@Column(name = "DESC", nullable = false)
 public String getDesc() {
  return this.desc;
 }
When insert into category table, hits following error message :
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 
   You have an error in your SQL syntax; check the manual that corresponds to your 
   MySQL server version for the right syntax to use near 'DESC) 
   values ('CONSUMER', 'CONSUMER COMPANY')' at line 1
   ... 35 more

Solution

In Hibernate, to insert into “keyword” column name, you should enclose it like this ‘[column name]‘.
Hibernate XML mapping file
<hibernate-mapping>
    <class name="com.mkyong.stock.Category" table="category" catalog="mkyongdb">
        ...
        <property name="desc" type="string">
            <column name="[DESC]" not-null="true" />
        </property>
       ...
    </class>
</hibernate-mapping>
Or Hibernate annotation
@Column(name = "[DESC]", nullable = false)
 public String getDesc() {
  return this.desc;
 }

Thursday, 19 February 2015

Error : Could not find or load main class

Very basic error i got during programming time in eclipse IDE. Where simple main class cannot run and give bellow error. I found one solution for it.

Error : Could not find or load main class

1) Right click on your project directory. 
2) Go to "Properties"
3) Select "Java Build Path" option from left side list.
4) Check "Allow output folders for source folder" at the bottom of the window.
5) Run again your class.

This is a one of the many way used for this error. Please comment if you solve by another way to help others.

Saturday, 14 February 2015

How to disable keyboard on JSP page using Java Script


In some cases you need to disable keyboard in your JSP (web page) page. Here is the simple example using Java Script you can do it.

<script type="text/javascript">
document.onkeydown = function (e) {
    if(e.which == 49){
            return false;
    }
}
</script> 

Here is the key code for all the key of the Keyboard. Use it with above code.

KEY CODE KEY CODE
backspace 8 tab 9
enter 13 shift 16
ctrl 17 alt 18
pause/break 19 caps lock 20
escape 27 page up 33
page down 34 end 35
home 36 left arrow 37
up arrow 38 right arrow 39
down arrow 40 insert 45
delete 46 0 48
1 49 2 50
3 51 4 52
5 53 6 54
7 55 8 56
9 57 a 65
b 66 c 67
d 68 e 69
f 70 g 71
h 72 i 73
j 74 k 75
l 76 m 77
n 78 o 79
p 80 q 81
r 82 s 83
t 84 u 85
v 86 w 87
x 88 y 89
z 90 left window key 91
right window key 92 select key 93
numpad 0 96 numpad 1 97
numpad 2 98 numpad 3 99
numpad 4 100 numpad 5 101
numpad 6 102 numpad 7 103
numpad 8 104 numpad 9 105
multiply 106 add 107
ctrl 17 alt 18
subtract 109 decimal point 110
divide 111 f1 112
f2 113 f3 114
f4 115 f5 116
f6 117 f7 118
f8 119 f9 120
f10 121 f11 122
f12 123 num lock 144
scroll lock 145 semi-colon 186
equal sign 187 comma 188
dash 189 period 190
forward slash 191 grave accent 192
open bracket 219 back slash 220
close braket 221 single quote 222



Tuesday, 3 February 2015

Apache Shiro Introduction - Basic

Apache Shiro
What it is?
In a one line we can say that ‘It is a powerful and easy to use Java security framework.’

Apache shiro is a powerful and flexible open - source security framework that cleanly handles authentication, authorization, enterprise session management and cryptography.

What can do with shiro?
lAuthenticate a user to verify their identity.
lPerform access control for the user, such as :
nDetermine if a user is assigned a certain security role or not.
nDetermine if a user is permitted to do something or not.
lUse a session API in any environment, even without web or EJB container.
lReact to events during authentication, access control, or during a session’s lifetime.
lAggregate 1 or more data sources of user security data and present this all as a single composite user 'view'.
lEnable Single Sign On (SSO) functionality.
lEnable 'Remember Me' services for user association without login.
...
and much more - all integrated into a cohesive easy-to-use API.

Where it can be used?
Shiro attempts to achieve these goals for all application environments - from the simplest command line application to the largest enterprise applications, without forcing dependencies on other 3rd party frameworks, containers, or application servers.

Detailed feature of Shiro :
Apache Shiro is a comprehensive application security framework with many features. The following diagram shows where Shiro focuses its energy, and this reference manual will be organized similarly:



lAuthentication: Sometimes referred to as 'login', this is the act of proving a user is who they say they are. 
lAuthorization: The process of access control, i.e. determining 'who' has access to 'what'. 
lSession Management: Managing user-specific sessions, even in non-web or EJB applications. 
lCryptography: Keeping data secure using cryptographic algorithms while still being easy to use.
There are also additional features to support and reinforce these concerns in different application environments, especially:
lWeb Support: Shiro's web support APIs help easily secure web applications.
lCaching: Caching is a first-tier citizen in Apache Shiro's API to ensure that security operations remain fast and efficient.
lConcurrency: Apache Shiro supports multi-threaded applications with its concurrency features.
lTesting: Test support exists to help you write unit and integration tests and ensure your code will be secured as expected.
l"Run As": A feature that allows users to assume the identity of another user (if they are allowed), sometimes useful in administrative scenarios.
l"Remember Me": Remember users' identities across sessions so they only need to log in when mandatory.

Feature for Web App :
lSimple ShiroFilter web.xml definition : You can enable Shiro for a web application with one simple filter definition in web.xml.

lProtects all URLs : Shiro can protect any type of web request that comes into your system. For example, dynamically generated pages, REST request, etc.

lInnovative Filtering (URL-specific chains) : Defining URL specific filter chains is much easier and more intuitive than using web.xml because, in Shiro, you can explicitly specify which filters you want to execute for each path and in what order. And with Shiro you can have path-specific configuration for each filter in that chain.

lJSP Tag support : The JSP tags allow you to easily control page output based on the current user’s state and access rights.

lTransparent HttpSession support : If you are using Shiro’s native sessions, we have implemented HTTP Session API and the Servlet 2.5 API so you don’t have to change any of your existing web code to use Shiro.