为什么我们使用Session session = entityManager.unwrap(Session.class)?

时间:2015-12-29 06:41:35

标签: java dao

我正在写一个DAO文件并看到它被使用了很多次但是没有明确我们为什么使用它以及它的目的是什么。

1 个答案:

答案 0 :(得分:0)

12-29 16:04:22.480: D/gralloc_goldfish(1319): Emulator without GPU emulation detected. 12-29 16:19:55.630: D/GPS Enabled(1319): GPS Enabled 12-29 16:19:55.650: D/AndroidRuntime(1319): Shutting down VM 12-29 16:19:55.650: W/dalvikvm(1319): threadid=1: thread exiting with uncaught exception (group=0xb39feba8) 12-29 16:19:55.700: E/AndroidRuntime(1319): FATAL EXCEPTION: main 12-29 16:19:55.700: E/AndroidRuntime(1319): Process: com.example.gpstracking, PID: 1319 12-29 16:19:55.700: E/AndroidRuntime(1319): java.lang.NullPointerException 12-29 16:19:55.700: E/AndroidRuntime(1319): at com.example.gpstracking.AndroidGPSTrackingActivity$1.onClick(AndroidGPSTrackingActivity.java:46) 12-29 16:19:55.700: E/AndroidRuntime(1319): at android.view.View.performClick(View.java:4438) 12-29 16:19:55.700: E/AndroidRuntime(1319): at android.view.View$PerformClick.run(View.java:18422) 12-29 16:19:55.700: E/AndroidRuntime(1319): at android.os.Handler.handleCallback(Handler.java:733) 12-29 16:19:55.700: E/AndroidRuntime(1319): at android.os.Handler.dispatchMessage(Handler.java:95) 12-29 16:19:55.700: E/AndroidRuntime(1319): at android.os.Looper.loop(Looper.java:136) 12-29 16:19:55.700: E/AndroidRuntime(1319): at android.app.ActivityThread.main(ActivityThread.java:5001) 12-29 16:19:55.700: E/AndroidRuntime(1319): at java.lang.reflect.Method.invokeNative(Native Method) 12-29 16:19:55.700: E/AndroidRuntime(1319): at java.lang.reflect.Method.invoke(Method.java:515) 12-29 16:19:55.700: E/AndroidRuntime(1319): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 12-29 16:19:55.700: E/AndroidRuntime(1319): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 12-29 16:19:55.700: E/AndroidRuntime(1319): at dalvik.system.NativeStart.main(Native Method) 12-29 16:19:59.060: I/Process(1319): Sending signal. PID: 1319 SIG: 9 包围Activity(以及其他内容)。

来自package com.example.gpstracking; import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class AndroidGPSTrackingActivity extends Activity { Button btnShowLocation; // GPSTracker class GPSTracker gps; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnShowLocation = (Button) findViewById(R.id.btnShowLocation); // show location button click event btnShowLocation.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // create class object gps = new GPSTracker(AndroidGPSTrackingActivity.this); // check if GPS enabled if(gps.canGetLocation()){ double latitude = gps.getLatitude(); double longitude = gps.getLongitude(); SQLiteDatabase dbm = this.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("latitude", gps.getLatitude()); cv.put("longitude", gps.getLongitude()); long result = dbm.insert("location",null,cv); Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); }else{ gps.showSettingsAlert(); } } private SQLiteDatabase getWritableDatabase() { // TODO Auto-generated method stub return null; } }); } }

的EntityManager文档
  

返回指定类型的对象以允许访问特定于提供程序的API。如果提供程序的EntityManager实现不支持指定的类,则抛出PersistenceException。

所以在你的情况下你知道 entityManager有一个Session并且想要使用它 - 你"打开它"进入那个班级。

这是必要的,因为投射无效