Hibernate使用查询(Hibernate working with queries)
开始学习hibernate。 通过教程做所有事情,但当我尝试做简单的选择时,没有任何反应。 不明白问题是什么。
日志:
мар 28, 2016 12:54:44 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.1.0.Final} мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist мар 28, 2016 12:54:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/taskboard] мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) мар 28, 2016 12:54:45 PM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect мар 28, 2016 12:54:45 PM org.hibernate.validator.internal.util.Version <clinit> INFO: HV000001: Hibernate Validator 5.2.4.Final мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/taskboard]
User.java
@Entity @Table(name="Users") public class User { @Id @Column(name="id_user") private Long id; @Column(name="first_name") private String firstName; @Column(name="last_name") private String lastName; @Column(name="login") private String login; @Column(name="password") private String password; }//get&set
Main.java
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); List<User> users = null; try { session.beginTransaction(); Criteria criteria = session.createCriteria(User.class); users = criteria.list(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { session.close(); sessionFactory.close(); } for (User user: users) { System.out.println(user.toString()); }
的hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/taskboard</property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping class="User" /> </session-factory> </hibernate-configuration>
HibernateUtil.java
public class HibernateUtil { private static SessionFactory sessionFactory = null; static { Configuration cfg = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()); sessionFactory = cfg.buildSessionFactory(builder.build()); } public static SessionFactory getSessionFactory(){ return sessionFactory; } }
Started to learn hibernate. Doing everything by tutorial, but when I try to do simple select, nothing happens. Сan't understand what the problem is.
Logs:
мар 28, 2016 12:54:44 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.1.0.Final} мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found мар 28, 2016 12:54:44 PM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist мар 28, 2016 12:54:45 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!) мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001005: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/taskboard] мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001001: Connection properties: {user=root, password=****} мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH10001003: Autocommit mode: false мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.PooledConnections <init> INFO: HHH000115: Hibernate connection pool size: 20 (min=1) мар 28, 2016 12:54:45 PM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect мар 28, 2016 12:54:45 PM org.hibernate.validator.internal.util.Version <clinit> INFO: HV000001: Hibernate Validator 5.2.4.Final мар 28, 2016 12:54:45 PM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl stop INFO: HHH10001008: Cleaning up connection pool [jdbc:mysql://localhost:3306/taskboard]
User.java
@Entity @Table(name="Users") public class User { @Id @Column(name="id_user") private Long id; @Column(name="first_name") private String firstName; @Column(name="last_name") private String lastName; @Column(name="login") private String login; @Column(name="password") private String password; }//get&set
Main.java
SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); List<User> users = null; try { session.beginTransaction(); Criteria criteria = session.createCriteria(User.class); users = criteria.list(); session.getTransaction().commit(); } catch (Exception e) { session.getTransaction().rollback(); e.printStackTrace(); } finally { session.close(); sessionFactory.close(); } for (User user: users) { System.out.println(user.toString()); }
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/taskboard</property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping class="User" /> </session-factory> </hibernate-configuration>
HibernateUtil.java
public class HibernateUtil { private static SessionFactory sessionFactory = null; static { Configuration cfg = new Configuration().configure(); StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() .applySettings(cfg.getProperties()); sessionFactory = cfg.buildSessionFactory(builder.build()); } public static SessionFactory getSessionFactory(){ return sessionFactory; } }
原文:https://stackoverflow.com/questions/36260161
更新时间:2022-11-18 10:11
最满意答案
您可以使用:
- (void)applicationWillResignActive:(UIApplication *)application { [[[self.tabBarController.viewControllers objectAtIndex:0] locationManager] stopUpdatingLocation]; //if it is your first VC, if not change objectAtIndex: to the correct one }
You can use:
- (void)applicationWillResignActive:(UIApplication *)application { [[[self.tabBarController.viewControllers objectAtIndex:0] locationManager] stopUpdatingLocation]; //if it is your first VC, if not change objectAtIndex: to the correct one }
相关问答
更多-
如何使iPhone4和以下版本的应用程序兼容iPhone5?(How to make iPhone4 and below version app compatible for iPhone5?)[2022-10-21]
这是我的答案, 1)我是否必须重建iPhone 5的应用程序? 如果你想利用完整的屏幕空间,你可以添加Default-568h@2x.png并重建应用程序并释放它。 2)我必须仅在UI中进行更改吗? 您可以利用iphone 5的完整屏幕空间。您可以编写其他iPhone的屏幕修改代码。 3)iPhone 5会使它兼容吗? 是。 它会使它自动兼容。 它会在顶部和底部添加一个黑色条以补偿屏幕上的额外空间,除非您添加Default-568h@2x.png 。 Here is my answers, 1) Do i ... -
最有可能使用OpenGL,如下所示: glBegin(GL_POINTS); glColor3f(...); glVertex3f(...); ... glEnd(); 更快的可能是使用顶点数组来指定点。 Most probably using OpenGL, something like: glBegin(GL_POINTS); glColor3f(...); glVertex3f(...); ... glEnd(); Even faster would probably be to use vert ...
-
你打算支持iOS 5或更早版本吗? 如果没有,我建议学习Autolayout。 使用此功能,您可以设置约束,以确保设备之间的布局看起来相似(只有更多的空间用于您的视图)。 首先,阅读本文以了解针对不同设备的推荐可用性建议(向下滚动到“在iPhone 5上运行”部分): http : //developer.apple.com/library/ios/#documentation/userexperience/conceptual/mobilehig /TranslateApp/TranslateApp.ht ...
-
iPhone 6s的iPhone 6s Plus尺寸屏幕被切断(iPhone 6s Plus size screen getting cut off on iPhone 6 Plus)[2024-03-08]
您可能想要使用自动布局 https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/AutolayoutPG/ 使用调试屏幕 po [[UIWindow keyWindow] _autolayoutTrace] 检查用户是否使用iPhone 6或6 Plus iOS如何通过宏检测iPhone 6 plus,iPhone 6,iPhone 5? You may want to use Auto Layout ... -
你可以在以下条件下轻松检测iphone,iphone5和ipad: - if([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPhone) { if ([[UIScreen mainScreen] bounds].size.height == 568) { } else { //iphone 3.5 inch screen } } el ...
-
iPhone 4上的视网膜显示器的像素数量是旧iPhone的两倍。 所以iPhone 4屏幕的完整尺寸(以像素为单位)为640x960 。 在Photoshop中,您可以通过从“图像”菜单更改图像大小属性来快速调整任何图像的大小。 如果您不想失去质量,我建议您从所有视网膜图像开始,并将它们减半,用于非视网膜显示。 以相反的方式这样做会降低你的质量。 如果您想使用专为iPhone 4视网膜显示器设计的图像,您需要在Xcode项目的资源文件夹中同时拥有两张图片,一张用于普通iPhone屏幕( 320x480 ) ...
-
尽管从可用性的角度来看它可能是有意义的,但我怀疑用灰色视图重叠窗口实际上会缩短电池的运行时间。 你将不得不调暗背光,这是主要的能量消耗。 这只适用于越狱手机或私人API(这会阻止您的应用获得Apple的批准)。 在视图控制器中使用此方法,通过添加50%alpha的黑色视图来调暗。 确保设置userInteractionEnabled = NO将事件传递给底层视图。 - (IBAction)dim:(id)sender { UIView *dimView = [[UIView alloc] initW ...
-
您要求以横向模式启动应用程序。 在applicationDidFinishedLaunchin:方法中添加此方法 [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; 并设计480x320尺寸的视图。 要使用相机,您需要在开发人员文档中探索可用的API。 请参阅此链接以获取UIImagePickerController类参考 You are ...
-
您可以使用: - (void)applicationWillResignActive:(UIApplication *)application { [[[self.tabBarController.viewControllers objectAtIndex:0] locationManager] stopUpdatingLocation]; //if it is your first VC, if not change objectAtIndex: to the correct one ...
-
如何在iPhone的“设置”屏幕中放置我的应用程序的设置(How to place my application's Settings in iPhone's “Settings” screen)[2024-01-19]
Apple的文档是最好看的地方。 该链接包含大量信息,甚至是一本帮助您入门的教程。 Apple's documentation is the best place to look. That link has loads of information, and even a tutorial to get you started.