Error
Cannot delete or update a parent row: a foreign key constraint fails
Issue:
mysql> update User SET id=6 where id = 8;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`database_name`.`table_name`, CONSTRAINT `FK_35jtu0049tkg8m9twhc03ii9` FOREIGN KEY (`customer`) REFERENCES `user` (`id`))
Solution:
SET foreign_key_checks = 1;
; execute code with constraint restrictions here
SET foreign_key_checks = 0;
Friday, April 4, 2014
Hibernate: setting foreigh key without getting foreign object
A nice solution from stackoverflow:
http://stackoverflow.com/questions/21012293/hibernate-add-entity-with-child-entity-id
http://stackoverflow.com/questions/21012293/hibernate-add-entity-with-child-entity-id
Tuesday, March 25, 2014
Java IO Tutorial - file read / write
Commonly Used Methods for Small Files
Reading All Bytes or Lines from a File
If you have a small-ish file and you would like to read its entire contents in one pass, you can use the readAllBytes(Path) or readAllLines(Path, Charset) method. These methods take care of most of the work for you, such as opening and closing the stream, but are not intended for handling large files. The following code shows how to use the readAllBytes method:Path file = ...;
byte[] fileArray;
fileArray = Files.readAllBytes(file);
Writing All Bytes or Lines to a File
The following code snippet shows how to use a write method.Path file = ...;
byte[] buf = ...;
Files.write(file, buf);
Original tutorial is locate here: http://docs.oracle.com/javase/tutorial/essential/io/file.html
Thursday, March 20, 2014
Getting last element in Hibernate's repository by annotations
Getting last element in hibernate is useful for integration testing, where you have to ensure some object is stored in database, for example.
Using Hibernate SQL.
Code:
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.Query;
public interface MyObjectRepository extends CrudRepository<MyObject, Long> {
@Query("FROM com.yourcompany.zzz.MyObject c WHERE (c.id) IN (select max(id) from com.yourcompany.zzz.MyObject)")
MyObject getLastOne();
}
It returns null, when there are no elements in table (do not throw an exception).
Using Hibernate SQL.
Code:
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.jpa.repository.Query;
public interface MyObjectRepository extends CrudRepository<MyObject, Long> {
@Query("FROM com.yourcompany.zzz.MyObject c WHERE (c.id) IN (select max(id) from com.yourcompany.zzz.MyObject)")
MyObject getLastOne();
}
It returns null, when there are no elements in table (do not throw an exception).
Tuesday, March 11, 2014
Wednesday, March 5, 2014
Blank template JavaScript application for JSON requests
Blank template application for JSON requests with and without body:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>My Blank JS / jQuery app - jsFiddle demo by jorooo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
<!--link rel="stylesheet" type="text/css" href="/css/result-light.css"-->
<script type='text/javascript' src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css">
<style type='text/css'>
</style>
</head>
<body>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("#alert").bind("click", function(event) {
alert('alert called');
});
$("#call_json_with_body").bind("click", function(event) {
$.ajax({
success: function( data ){
alert("success");
alert('success, data received is ' + JSON.stringify(data));
},
error: function( jqXHR, textStatus, errorThrown ){
alert( "error while making ajax call. Error parameters are, " +
"\ntextStatus: " + textStatus +
";\n errorThrown: " + errorThrown +
";\n jqXHR: " + JSON.stringify(jqXHR) );
},
beforeSend: function(req) {
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type","application/json");
},
dataType: 'json',
data: JSON.stringify( {"zzz":"222"} ),
type: 'POST',
cache: false,
url: "http://localhost:8080/add_venue_raw",
}
);
});
$("#call_json_without_body").bind("click", function(event) {
$.ajax({
success: function( data ){
alert("x");
alert('success, data received is ' + JSON.stringify(data) );
},
error: function( jqXHR, textStatus, errorThrown ){
alert( "error while making ajax call. Error parameters are, " +
"\ntextStatus: " + textStatus +
";\n errorThrown: " + errorThrown +
";\n jqXHR: " + JSON.stringify(jqXHR) );
},
type: 'GET',
cache: false,
url: "http://localhost:8080/user/1",
}
);
});
});//]]>
</script>
<div data-role="page" id="p1">
<div data-role="header"><h1>Header</h1></div>
<div data-role="content">
<h5>If the script is running from a file:/// URL you could experience some issues. It's best not to do AJAX requests from file URLs, because they are treated inconsistently. Chrome, for example, entirely disallows them. Firefox also.</h5>
<a href="#" id="call_json_without_body" data-role="button">JSON request without body</a><br/>
<a href="#" id="call_json_with_body" data-role="button">JSON request with body</a><br/>
<a href="#" id="alert" data-role="button">Show alert</a>
</div>
<div data-role="footer"><h4>Footer</h4></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>My Blank JS / jQuery app - jsFiddle demo by jorooo</title>
<script type='text/javascript' src='http://code.jquery.com/jquery-latest.js'></script>
<!--link rel="stylesheet" type="text/css" href="/css/result-light.css"-->
<script type='text/javascript' src="http://code.jquery.com/mobile/latest/jquery.mobile.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/mobile/latest/jquery.mobile.min.css">
<style type='text/css'>
</style>
</head>
<body>
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$("#alert").bind("click", function(event) {
alert('alert called');
});
$("#call_json_with_body").bind("click", function(event) {
$.ajax({
success: function( data ){
alert("success");
alert('success, data received is ' + JSON.stringify(data));
},
error: function( jqXHR, textStatus, errorThrown ){
alert( "error while making ajax call. Error parameters are, " +
"\ntextStatus: " + textStatus +
";\n errorThrown: " + errorThrown +
";\n jqXHR: " + JSON.stringify(jqXHR) );
},
beforeSend: function(req) {
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type","application/json");
},
dataType: 'json',
data: JSON.stringify( {"zzz":"222"} ),
type: 'POST',
cache: false,
url: "http://localhost:8080/add_venue_raw",
}
);
});
$("#call_json_without_body").bind("click", function(event) {
$.ajax({
success: function( data ){
alert("x");
alert('success, data received is ' + JSON.stringify(data) );
},
error: function( jqXHR, textStatus, errorThrown ){
alert( "error while making ajax call. Error parameters are, " +
"\ntextStatus: " + textStatus +
";\n errorThrown: " + errorThrown +
";\n jqXHR: " + JSON.stringify(jqXHR) );
},
type: 'GET',
cache: false,
url: "http://localhost:8080/user/1",
}
);
});
});//]]>
</script>
<div data-role="page" id="p1">
<div data-role="header"><h1>Header</h1></div>
<div data-role="content">
<h5>If the script is running from a file:/// URL you could experience some issues. It's best not to do AJAX requests from file URLs, because they are treated inconsistently. Chrome, for example, entirely disallows them. Firefox also.</h5>
<a href="#" id="call_json_without_body" data-role="button">JSON request without body</a><br/>
<a href="#" id="call_json_with_body" data-role="button">JSON request with body</a><br/>
<a href="#" id="alert" data-role="button">Show alert</a>
</div>
<div data-role="footer"><h4>Footer</h4></div>
</div>
</body>
</html>
static root folder for maven projects
Static files could be copied here:
<maven-project-root>\src\main\webapp\resources
They are accessed on server on URL:
<server_address>/resources
Example:
<maven-project-root>\src\main\webapp\resources\x.txt
could be accessed on
http://localhost:8080/resources/x.txt
In come cases, the content changes are actualized without server restart. For example with:
mvn jetty:run
<maven-project-root>\src\main\webapp\resources
They are accessed on server on URL:
<server_address>/resources
Example:
<maven-project-root>\src\main\webapp\resources\x.txt
could be accessed on
http://localhost:8080/resources/x.txt
In come cases, the content changes are actualized without server restart. For example with:
mvn jetty:run
Subscribe to:
Posts (Atom)