Tag: hibernate

JPQL在select语句中创build新对象 – 避免还是拥抱?

我最近了解到,可以在JPQL语句中创build新的对象,如下所示: select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr 这是要避免还是要拥抱? 根据良好的做法,何时使用这个function是合理的?

在使用JPA和Hibernate时应该如何实现equals和hashcode

应该如何在Hibernate中实现模型类的equals和hashcode? 什么是常见的陷阱? 在大多数情况下,默认实现是否足够好? 有没有使用商业密钥的意义? 在我看来,在任何情况下都很难做到这一点,比如懒惰的抓取,身份证生成,代理等等。

ORM映射中的“拥有方”是什么?

拥有方意味着什么? 什么是一些映射实例的解释( 一对多,一对一,多对一 )? 以下文字摘自Java EE 6文档中@OneToOne的描述。 你可以看到它拥有一面的概念。 定义一个单值关联到另一个具有一对一多重性的实体。 通常不需要显式指定关联的目标实体,因为通常可以从被引用的对象的types中推断出来。 如果关系是双向的, 则非拥有方必须使用OneToOne批注的mappedBy元素来指定拥有方的关系字段或属性。

同步静态方法在Java中如何工作?

如果我有一个具有静态方法的util类,它将调用Hibernate函数来完成基本的数据访问。 我想知道是否使方法synchronized是确保线程安全的正确方法。 我想这样做,以防止信息访问相同的数据库实例。 不过,我现在确定下面的代码是否阻止getObjectById被所有类调用,当它被一个特定的类调用时。 public class Utils { public static synchronized Object getObjectById (Class objclass, Long id) { // call hibernate class Session session = new Configuration().configure().buildSessionFactory().openSession(); Object obj = session.load(objclass, id); session.close(); return obj; } // other static methods }

PersistenceContext EntityManager注入NullPointerException

我有一个包含以下内容的战争: META-INF/MANIFEST.MF WEB-INF/classes/META-INF/persistence.xml WEB-INF/classes/com/test/service/TestServlet.class WEB-INF/classes/com/test/service/TestEntity.class WEB-INF/classes/jndi.properties WEB-INF/classes/postgresql-ds.xml WEB-INF/jboss-web.xml WEB-INF/web.xml index.jsp persistence.xml中: <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="test"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/TestDS</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.show_sql" value="true" /> </properties> </persistence-unit> </persistence> web.xml中: <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Test Web Application</display-name> […]

用hibernate.enable_lazy_load_no_trans解决Hibernate的Lazy-Init问题

我一直在遭受臭名昭着的hibernateexception org.hibernate.LazyInitializationException: could not initialize proxy – no Session 现在社区正在欢呼 <property name="hibernate.enable_lazy_load_no_trans" value="true"/> 说它解决了这个问题,但使用它注意 。 他们是什么意思,谨慎使用它? 这个属性实际上做了什么? 请给我任何见解。 提前致谢。

hibernate:根据实体类自动创build/更新数据库表

我有以下实体类(在Groovy中): import javax.persistence.Entity import javax.persistence.Id import javax.persistence.GeneratedValue import javax.persistence.GenerationType @Entity public class ServerNode { @Id @GeneratedValue(strategy = GenerationType.AUTO) Long id String firstName String lastName } 和我的persistence.xml: <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"> <persistence-unit name="NewPersistenceUnit"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/Icarus"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.archive.autodetection" value="class"/> <property name="hibernate.show_sql" value="true"/> <property […]

如何使用JPA和Hibernate在UTC时区存储date/时间和时间戳

如何configurationJPA / Hibernate作为UTC(GMT)时区在数据库中存储date/时间? 考虑这个带注释的JPA实体: public class Event { @Id public int id; @Temporal(TemporalType.TIMESTAMP) public java.util.Date date; } 如果date是2008年2月3日上午9:30太平洋标准时间(PST),那么我希望2008年2月3日下午5:30的UTC时间存储在数据库中。 同样,当从数据库中检索date时,我希望它解释为UTC。 所以在这种情况下,530pm是UTC530。 显示时,它将被格式化为太平洋标准时间上午9:30。

有人可以请解释映射在hibernate?

我是hibernate的新手,需要使用1-Many和Many-1的关系。 这是我的对象中的双向关系,所以我可以从任何一个方向进行遍历。 mappedBy是推荐的方法。 但是,我不明白这一点。 有人可以请给我解释一下, 推荐使用的方法是什么? 它解决了什么目的? 为了我的例子,这里是我的注释类: Airline 许多 Airline 许多 AirlineFlights属于AirlineFlights Airline 航空公司 : @Entity @Table(name="Airline") public class Airline { private Integer idAirline; private String name; private String code; private String aliasName; private Set<AirlineFlight> airlineFlights = new HashSet<AirlineFlight>(0); public Airline(){} public Airline(String name, String code, String aliasName, Set<AirlineFlight> flights) { setName(name); setCode(code); setAliasName(aliasName); […]

解决“未能懒散地初始化一个angular色集合”的例外

我有这个问题: org.hibernate.LazyInitializationException:未能延迟初始化angular色集合:mvc3.model.Topic.comments,没有会话或会话已closures 这是模型: @Entity @Table(name = "T_TOPIC") public class Topic { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @ManyToOne @JoinColumn(name="USER_ID") private User author; @Enumerated(EnumType.STRING) private Tag topicTag; private String name; private String text; @OneToMany(mappedBy = "topic", cascade = CascadeType.ALL) private Collection<Comment> comments = new LinkedHashSet<Comment>(); … public Collection<Comment> getComments() { return comments; } } 调用模型的控制器如下所示: @Controller @RequestMapping(value […]