Tag Archives: null

The solution of Hibernate query returning all null lists

Today, when I was working on a project, I used HQL to look up the database, but all the returned results were null. Note that the returned list is not null, but there are values in the list. The size of the list is the total number of records, but when I traverse the list output, all the elements in the list are null. I suspect that there is something wrong with my conditions. I have removed all the conditions, leaving only one sentence:

String hql = "from CarMaintain t";

It’s worth the rest of the sentence, but it’s still the result. I searched the information on the Internet, and all of them were wrong answers. Without exception, I said it was impossible… Then how did I meet… Others are unreliable, so we have to find our own problems.

I began to analyze, why only this sentence has this situation, and other sentences are OK?I found that the table in question has no primary key. Yes, that’s the reason. When there is no primary key, hibernate’s reverse engineering will automatically construct a composite primary key for us, so I started to check hbm.xml The file and configuration are correct. What’s the problem?I collapsed all afternoon for it. The reason is due to the internal mechanism of hibernate,

In my table without primary key, the fields in the composite primary key constructed by hibernate have null values, so no matter how you look it up, it is null in the end. When you break the point, you will see [null, null, null,….]

resolvent:

1. Create a new field, use it as the primary key, and hbm.xml The file is modified correctly

2. All fields contained in the composite primary key cannot be null in the database

It took a long time to solve this problem. I hope I can help you.

The problem that the content extra data in the notification cannot be updated

Recently younger brother when writing a find out about the background data processing service within the PendingIntent NotificationManager to send the Notification of the Intent of the Extra data cannot update, unable to get even, after tests found that the same ID Notification first Intent can only be stored data, even if use the NotificationManager. Cancel also of no help.
Found after examined the related API, use the PendingIntent. GetActivity (mContext, 0, mActivity, 0); After the Notification is opened, the target Activity cannot retrieve any additional data from getIntent(). Set the flag of the getActivity function to either pendingintent.flag_update_current or pendingintent.flag_cancel_current according to the API.
Also put a PendingIntent, getActivity function can be used in several flag value meaning:

0:

Default value, if the described PendingIntent already exists, then keep it without change.

FLAG_CANCEL_CURRENT:

For use with a Flag with the getActivity (Context, int, Intent, int) , getBroadcast (Context, int, Intent, int) , and getService (Context, int, Intent, int) : if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; By canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAT_Update_current

Constant Value: 268435456 (0 x10000000)

FLAG_NO_CREATE:

For use with a Flag with the getActivity (Context, int, Intent, int) , getBroadcast (Context, int, Intent, int) , and getService (Context, int, Intent, int) : if the described PendingIntent does not already exist, then simply return null instead of creating it.

Constant Value: 536870912 (0 x20000000)

FLAG_ONE_SHOT:

For use with a Flag with the getActivity (Context, int, Intent, int) , getBroadcast (Context, int, Intent, int) , and getService (Context, int, Intent, int) : This PendingIntent can only be used once. If set, after send()

FLAG_UPDATE_CURRENT:

For use with a Flag with the getActivity (Context, int, Intent, int) , getBroadcast (Context, int, Intent, int) , and getService (Context, int, Intent, int) : if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

Constant Value: 134217728 (0 x08000000)

0xc0000005: an access conflict occurred while reading location 0x00000020

0x in.exe… Unhandled exception at: 0xC0000005: Access conflict occurred while reading position 0x00000020

Error: : PostMessage (AfxGetMainWnd () – & gt; M_hWnd, UM_XX, 0, 0);

or: : sendMessage (
AfxGetMainWnd()-> M_hWnd, UM_XX, 0, 0);

How to solve it?
Verify that afxGetMainWnd () returns NULL

int k = (int)(& (((CWnd*)NULL)-> m_hWnd));
The value of k is 0x20

In general: afXGetApp ()-> GetMainWnd (); AfxGetMainWnd() is used only on the main thread;

How: foreground thread and background thread, afxGetApp ->; GetMainWnd() differs from AFXGetMainWnd ()

AFXGetMainWnd () is an example of a window handle that can be used to retrieve a window handle. AFXGetMainWnd () is an example of a window handle that can be used to retrieve a window handle. Clever you must have already understand, if you have been tracking AfxGetMainWnd () call, will find that it earned a AFX_MODULE_THREAD_STATE threading module save active threads in the window handle, and a background thread since no window, then obtains the window handle is you from??(maybe someone’s understanding of the background is not display window, even with all the window, as long as it doesn’t show is a background thread, strictly speaking is not the case, the window is mainly used to interact with the user, a window is hard to avoid jam, And the background thread is often used to carry out some behind the operation or processing, is through the foreground information or data to carry out the corresponding operation, if the window is hidden to talk about what information transfer?The case can not be done in the background like other threads. The key is to know the difference and how to use it.)
AfxGetApp()-> GetMainWnd () is the main window handle, no matter in that thread calls are no problem, because it is first obtained the main thread handle, to achieve the main thread of the active window (such as switch view can lead to replacement, I am not sure this kind of circumstance), if there is no active window is the main window, any program must have a main window, so it calls there is no problem, if you want to achieve the program’s main window suggested AfxGetApp () – & gt; GetMainWnd().
Note that the console program has no window and its window handle is always 0. Second, the background thread is actually a desktop control program, just not the main thread. Also, threads created using functions such as API CreateThread cannot generate CWinThread objects. So if you want to use CWinThread object function, and some of the global function, as AfxGetMainWnd (), you must use CWinThread object CreateThread function, or use the AfxBeginThread () function creates a thread, or may be an error, because the MFC to the management of the thread is done through the CWinThread object, it is not hard to see through the tracking code below you. I remember that there was a predecessor once mentioned this problem, but I do not know whether it understands the reason
Here is the trace code:
_AFXWIN_INLINE CWnd* AFXAPI AfxGetMainWnd()
{ CWinThread* pThread = AfxGetThread();
return pThread! = NULL ?pThread-> GetMainWnd() : NULL; }

CWinThread* AFXAPI AfxGetThread()
{
// check for current thread in module thread state
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
CWinThread* pThread = pState-> m_pCurrentWinThread;
// if no CwinThread for the module, then use the global app
if (pThread == NULL)
hread = afxGetApp ();
return pThread;
}
You can see that calling afxgetMainwnd () on the main thread is not a problem (except for console programs) because it returns the main thread handle if the fetched thread handle is empty.
_AFXWIN_INLINE CWinApp* AFXAPI AfxGetApp()
{ return afxCurrentWinApp; }

CWnd* CWinThread::GetMainWnd()
{
if (m_pActiveWnd ! = NULL)
return m_pActiveWnd;// probably – place in active
// when not inplace active, just return main window
if (m_pMainWnd ! = NULL)
return m_pMainWnd;
return CWnd::GetActiveWindow();
}
I don’t need to worry about AFXCurrentInApp.
Hey hey, all finished, if your program exists similar to the above problems can be sure to change back oh.

Android avoids memory leak: reasonable use of getcontext() and getapplication ()

To conclude:
GetApplicationContext () can fetch the Application object, and getContext() is generally considered to return an Activity object (which, of course, is not actually limited to the Activity). 2. For Application, you can see from the Manifest file that an Application typically has only one Application node. Application is just an Application, that is, as long as the current Application is still running, it can get to the Application object. 3.Application is a long reference, and Activity is a short reference. Application is suitable for storing objects that need to be read repeatedly, such as the user’s username and password, the current Settings of the Application, and so on. When an Activity is applied to the current active form, such as displaying a Dialog, or creating a new View, the context object passed in should be the current Activity, not the Application.
    
Android applications limit heap memory to 16M (note: heap memory is also related to device performance; available heap memory for high performance devices may be 24M or more), with phone functions taking up a portion of the heap and developers having very limited access to it. If you’re not going to run out of memory, your application should use as little as possible so that other programs don’t get killed when they run. The more applications the Android system can hold in memory, the faster users can switch between applications. As part of my work, I researched memory leaks in Android applications and found that most of the time it was the same problem: holding a long reference to the Context.
In Android, Context is used for many operations, but mostly to load or access resources. This is why all Widget components receive a Context parameter in the constructor. For a typical Android Application, there are usually two contexts, namely Activity and Application. Developers often need the context to select an Activity when passing between classes or methods.

this indicates that Views have a reference to the entire Activity, so anything in your Activity will be held by Views, which are generally held by the view hierarchy and its corresponding resources. So, if you give away the Context, you’re giving away some memory. If you’re not careful, it’s very easy to reveal an Activity.
when the screen direction changes, the system will save its state by default, destroy the current Activity, and create a new one. To do this, Android reloads the application’s UI from resources. Now let’s say you’re developing an app that contains a lot of bitmaps, and you don’t want to load bitmaps every time you switch screens. So the easiest way to do this is to use the static static field.

check a tree is balanced or not

Question:
Given a binary tree, check whether it is balanced or not.
Analyze:

Consider a height-balancing scheme where following conditions should be checked to determine if a binary tree is balanced.
An empty tree is height-balanced. A non-empty binary tree T is balanced if:
1) Left subtree of T is balanced
2) Right subtree of T is balanced
3) The difference between heights of left subtree and right subtree is not more than 1.
The above height-balancing scheme is used in AVL trees. The diagram below shows two trees, one of them is height-balanced and other is not. The second tree is not height-balanced because height of left subtree is 2 more than height of right subtree.

To check if a tree is height-balanced, get the height of left and right subtrees. Return true if difference between heights is not more than 1 and left and right subtrees are balanced, otherwise return false.
Code:

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isBalanced(TreeNode root) {
        if (root == null) return true;
        if (Math.abs(height(root.left) - height(root.right)) > 1) return false;
        return isBalanced(root.left) && isBalanced(root.right);
    }
    
    public int height(TreeNode root) {
        if (root == null) return 0;
        return Math.max(height(root.left), height(root.right)) + 1;
    }
}

The complexity of the code above is O(N^2). However, some interviewers may ask to provide solution with O(N) complexity. Below is the solution with O(N) complexity. The main idea is if the subtree is balanced, return the true height, else, return -1. Therefore, the parent node of that unbalanced subtree also has -1 height.

public class Solution {
    public boolean isBalanced(TreeNode root) {
        return getDepth(root) >= 0;
    }
    
    public int getDepth(TreeNode node) {
        if (node == null) return 0;
        int leftDepth = getDepth(node.left);
        if (leftDepth < 0) return -1;
        
        int rightDepth = getDepth(node.right);
        if (rightDepth < 0) return -1;
        
        if (Math.abs(leftDepth - rightDepth) > 1) return -1;
        return Math.max(leftDepth, rightDepth) + 1;
    }
}

Reference: 

http://www.geeksforgeeks.org/how-to-determine-if-a-binary-tree-is-balanced/
http://blog.theliuy.com/2012/balanced-binary-tree/

Some problems in using arrays

/*
* array note some issues
*/
#include < iostream>
using namespace std;
Int main()
{
const int ArraySize = 5;
// initializes the elements in the array, and the uninitialized elements are by default filled with the value 0, representing the NULL character (NULL or \0)
int arr[ArraySize] = {10,3,5};

// array name, which represents the starting address of the first element (direct array name is recommended)
cout < < “arr = ” < < arr < < endl;
// array is a collection, take the address of the array name, get is the first address of the collection represented by the array (generally not recommended to use the address of the array name)
cout < < “& arr = ” < < & arr < < endl;

{
/* array name itself is not a variable, so it does not occupy storage space, nor can it be used to store data; ,
, which simply represents a data, as a value, it represents the address of the first data element in the array. */
// for example, the following is wrong!!
/* compile times wrong: assigning to an array from the an, initializer list */
// arr = {10,16,8};
// therefore, arrays cannot be assigned, only array elements can be initialized or assigned.
}
// if an array is defined and not initialized, its output will be garbage instead of having no value
/*cout < < “No array initialization, garbage data output!” < < endl;
for(int I = 0; i ! = ArraySize; I++)
cout & lt; < arr[i] < < “‘;
cout & lt; < endl;

// input data, assign values to data elements in arr array
/* even if the number of input data elements is greater than the length of the array, cin will only extract required data from the input buffer according to the length of the array
, while the remaining data will be left in the input buffer for use next time */
//cout < < “The input data are:”;
//for(int I = 0; i ! = ArraySize; I++)
// cin & gt; > arr[i];

// below are three different ways to output the address of each array element in memory
/*cout < < “Method 1:” < < endl;
for(int I = 0; i ! = ArraySize; I++)
cout & lt; < “& arr[” << i << “] = ” < < & arr[i] < < endl;
cout & lt; < “Method two:” < < endl;
for(int I = 0; i ! = ArraySize; I++)
cout & lt; < “[arr+” << i << “] = ” < < arr+i < < endl;
cout & lt; < “Method 3:” < < endl;
for(int I = 0,*p = arr; p ! = (arr+ArraySize) & & i ! = ArraySize; I++ p++)
cout & lt; < “p -[” << i << “]= ” < < p < < endl; */

/* the following four methods of outputting elements in an array have no errors;
the compiler changes the brackets in the array at compile time :(both sides are equivalent to each other)
arr[I] < => * (arr + I)
* (arr + I) & lt; => * (I + arr)
* (I + arr) & lt; => I (arr)
arr [I] < => I [arr]
I is the offset, indicating the number of elements of the offset first element */
/*for(int I = 0; i ! = ArraySize; I++)
cout & lt; < arr[i] < < “‘;
cout & lt; < endl;
For (int I = 0; i ! = ArraySize; I++)
cout & lt; < *(arr+i) < < “‘;
cout & lt; < endl;
For (int I = 0; i ! = ArraySize; I++)
cout & lt; < i[arr] < < “‘;
cout & lt; < endl;

for(int* p = arr; p ! = (arr+ArraySize); P++)
cout & lt; < *p < < “‘;
cout & lt; < endl; * /

cout <
cout < < “The consequences of a cross-border visit!” < < endl;
int iNum = 123,iLen = 90;
int array[7] = {10,3,6,8,5,9,7};
for(int I = -2);
for(int I = -2);
for(int I = -2); i ! = 9; I++)
cout & lt; < array[i] < < “‘;
cout & lt; < endl;

cout & lt; < iNum < < “—” < < iLen < < endl;
// Out-of-bounds storage of data can result in the value of other variables, and may even cause the program to crash (which didn’t happen, which is weird!!).
for(int I = -2; i ! = 7. I++)
cin & gt; > array[i];
for(int I = 0; i ! = 7. I++)
cout & lt; < array[i] < < “‘;
cout & lt; < endl;
cout & lt; < iNum < < “—” < < iLen < < endl;
return 0;
}

error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)

Can the size of an array be defined at run time?
no. In the definition of an array, the size of the array must be known at compile time, not at run time. For example, if I is a variable, you cannot use I to define the size of an array:
char array[I]; Notvalidc */
supports this definition in some languages, but not in C. If C supported this definition, the stack would become more complex, the overhead of calling functions would be greater, and the program would run significantly slower.
if the size of the array is known at compile time, even if it is a very complex expression, you can define it as long as it can be calculated at compile time. If you want to use an array whose size is not known until the program is running, you can specify a pointer and call the malloc() or calloc() functions to allocate memory space for the array from the heap. Here is an example of an argv array that is copied to the main() function:

[cpp]
view plain
copy

    7.15 cases in row to determine the size of the array, use the pointer and the malloc ()/* A silly program that copies the argv array and all the pointed – to strings. Just for fun, it also deallocates all the copies. */# include & lt; Stdlib. H> # include & lt; String. H> Int main (int arg c, char * * argv) {char * * new_argv; Int I;/* Since argv [0] through argv [arg c] are all valid, the program needs to the allocate room for arg c + 1 Pointers. */new_argv = (char * *) calloc (arg c + l, sizeof (char *));/* or malloc ((arg c + 1) * sizeof (char *)) */printf (” allocated room for % d Pointers starting at % P \ n “, arg c + 1, new_argv);/* now copy all the strings more (argv [0] through argv [arg c – l]) */for (I = 0; i< argc; + + I) {/ * make room for ‘\ 0’ at end, too */new_argv [I] = (char *) malloc (strlen (argv [I]) + l); Strcpy (new_argv [I], argv [I]); Printf (” % d bytes allocated for new_argv [% d] at % P “, “copied \” % s \ \ “n”, strlen (argv [I]) + l, I, new_argv [I], new_argv [I]); } new_ argv [arg c] = NULL:/* To deallocate everything, get rid of the strings (in any order), then the array of Pointers. If you free the array of poiners first, you lose all the reference To t (I = 0); i< argc; + + I) {free (new_argv [I]); Printf (” freed new_argv [% d] at % P \ n “, I, new_argv [I]); Argv [I] = NULL; * * Habit, see note at the end of this example */} free(new_argv); Printf (” Freed new_argv itself at %P\n”, new_argv); Return 0; /* see 16.4 */}

Note: Why does example 7.5 assign NULL after freeing each element in the new_argv array?This is a habit formed on the basis of long practice. After a pointer is released, you can no longer use the data it originally pointed to, or the pointer is “suspended” and no longer points to any useful data. If a pointer is assigned NULL immediately after it is released, the program will not make an error even if it USES the pointer again. Of course, the program may indirectly refer to the null pointer, but such errors can be detected in time while debugging the program. In addition, a
program may still have some original copies of the pointer pointing to the portion of memory that has been freed, which is natural in C programs. In short, although this habit doesn’t solve all problems, it does help.

MySQL creates tables and sets auto increment of primary keys

mysql创建表:

mysql>创建表用户(
->userid int(4)主键not null auto_increment,
->用户名varchar(16)不为空,
->userpassword varchar(32) not null
-&gt

创建表log(
logid int(4) primary key not null auto_increment,
logtitle varchar(32) not null,
logcontent varchar(160) not null,
logtime datetime not null,
userip varchar(64) not null
)