标签: hibernate-annotations

Hibernate 4注释配置

我正在尝试使用仅带注释的Hibernate 4和一个hibernate.cfg.xml文件.我已经制作了自己的注释,并使用反射将其添加到配置中.我能够以这种方式使用Hibernate 4,但我的配置是使用不推荐的方法构建的.

final Configuration configuration = new Configuration();
final Reflections reflections = new Reflections(Item.class.getPackage().getName());
final Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Entity.class);
for (final Class<?> clazz : classes) {
    configuration.addAnnotatedClass(clazz);
}
return configuration.configure().buildSessionFactory();
Run Code Online (Sandbox Code Playgroud)

(不推荐的代码:) buildSessionFactory();.

即使是hibernate 4文档也显示以这种方式构建配置.

如果我尝试使用新方法(buildSessionFactory(ServiceRegistry)我没有得到相同的结果,似乎有很多不必要的代码完全按照不推荐使用的方法做的那样.但是,我不想继续使用这种风格,因为我不喜欢使用已弃用的代码.

我的问题是:如何以上述方式从配置文件中正确配置Hibernate 4?我似乎只是造成错误并面临不必要的困难.

java hibernate hibernate.cfg.xml hibernate-annotations hibernate-4.x

7
推荐指数
1
解决办法
6276
查看次数

Hibernate命名策略

我正在使用Spring(Boot)构建REST Web服务,并尝试使用hibernate作为orm mapper而不使用任何xml配置.

我基本上可以使用它,但我遇到了配置问题.我实例LocalContainerEntityManagerFactoryBean作为@Bean一个@Configuration文件.

hibernate.ejb.naming_strategy在下面的例子中设置 - >这似乎适用于创建表,如果它们不存在(列名是像我的@Entity类中的camelCase)但是当执行查询时,hibernate"忘记"关于这个命名配置和尝试使用另一种命名策略与under_score_attributes - >显然这些查询失败.我需要设置其他任何属性吗?

或者另一种配置属性的方法,最好不要添加cfg.xml或者persistence.xml

LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();   
Properties props = new Properties();
props.put("hibernate.hbm2ddl.auto", "create");
props.put("hibernate.ejb.naming_strategy","org.hibernate.cfg.DefaultNamingStrategy");
lef.setJpaProperties(props); 
lef.afterPropertiesSet();
Run Code Online (Sandbox Code Playgroud)

spring hibernate hibernate-annotations spring-boot

7
推荐指数
1
解决办法
3万
查看次数

Maven + Hibernate注释模式生成

我有一堆用hibernate注释注释的类.我正在使用Maven,Hibernate和Spring.如何使用hibernate3-maven-plugin的hbm2ddl生成数据库模式?

maven-2 annotations hibernate hibernate-mapping hibernate-annotations

6
推荐指数
1
解决办法
6094
查看次数

Java Hibernate映射异常!(无法确定类型:java.util.Map)

我创建了一个名为Movie的课程,其中包括以下字段:

    @Id
@GeneratedValue
private Long id;
private String name;
@ElementCollection(targetClass = String.class)
private Map<String, String> properties;
private Double rate;
private Integer votersCount;
private Date releaseDate;
private Integer runtime;
@ManyToMany
@JoinTable(name = "movie_director")
@IndexColumn(name = "directorIndex")
private List<Person> directors;
@ManyToMany
@JoinTable(name = "movie_writer")
@IndexColumn(name = "writerIndex")
private List<Person> writers;
@OneToMany
@IndexColumn(name = "roleIndex")
private List<MovieRole> movieRoles;
@ManyToMany
@JoinTable(name = "movie_genre")
@IndexColumn(name = "genreIndex")
private List<Genre> genres;
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我使用了hibernate注释,对象就是bean.但是当我尝试使用以下代码打开我的hibernate会话时......

session = HibernateSessionFactory.getSessionFactory().openSession();
Run Code Online (Sandbox Code Playgroud)

我遇到了一个无法映射Java.Util.Map类的问题.这是异常堆栈跟踪:

org.hibernate.MappingException: Could not determine type for: java.util.Map, for columns: [org.hibernate.mapping.Column(properties)] …
Run Code Online (Sandbox Code Playgroud)

java hibernate-mapping hibernate-annotations

6
推荐指数
1
解决办法
3万
查看次数

JPA Cascade注释与Hibernate Cascade注释之间的区别

我正在尝试使用Hibernate注释.当我尝试使用Cascading时,我在Eclipse intellisense中得到两个选项:

    javax.persistence.CascadeType and org.hibernate.annotations.CascadeType
Run Code Online (Sandbox Code Playgroud)

在休眠CascadeType中,给出了许多选项,而不是JPA的选项.

一个人有什么优势吗?

java hibernate hibernate-annotations hibernate-cascade

6
推荐指数
1
解决办法
4870
查看次数

如何在字段级别创建元注释?

我有这个带注释的hibernate类:

@Entity
public class SimponsFamily{

  @Id
  @TableGenerator(name = ENTITY_ID_GENERATOR,
                table = ENTITY_ID_GENERATOR_TABLE,
                pkColumnName = ENTITY_ID_GENERATOR_TABLE_PK_COLUMN_NAME,
                valueColumnName = ENTITY_ID_GENERATOR_TABLE_VALUE_COLUMN_NAME)
  @GeneratedValue(strategy = GenerationType.TABLE, generator = ENTITY_ID_GENERATOR)
  private long id;

  ...
}
Run Code Online (Sandbox Code Playgroud)

因为我不会以这种方式注释我的类的每个id字段,所以我尝试创建自定义anotation:

@TableGenerator(name = ENTITY_ID_GENERATOR,
            table = ENTITY_ID_GENERATOR_TABLE,
            pkColumnName = ENTITY_ID_GENERATOR_TABLE_PK_COLUMN_NAME,
            valueColumnName = ENTITY_ID_GENERATOR_TABLE_VALUE_COLUMN_NAME)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EntityId {

    @GeneratedValue(strategy = GenerationType.TABLE, generator = ENTITY_ID_GENERATOR)
    public int generator() default 0;

    @Id
    public long id() default 0;
}
Run Code Online (Sandbox Code Playgroud)

这样我就可以在课堂上使用这个注释:

 @Entity
 public class SimponsFamily{


 @EntityId
 private long id;

  ...
}
Run Code Online (Sandbox Code Playgroud)

我必须在字段级别编写 …

java annotations hibernate-annotations

6
推荐指数
1
解决办法
865
查看次数

Hibernate映射父/子关系

我有一个数据库表,其中包含一个简单的自我父/子关系(类别),如下所示:

+------------+----------+--------------------+
| categoryid | parentid | category_name      |
+------------+----------+--------------------+
|          1 |        0 | Java               |
|          2 |        0 | SKM                |
|          3 |        0 | Neuigkeiten        |
|          4 |        0 | Neue Versionen     |
|          5 |        0 | PlugIn             |
..
|          9 |        2 | Subversion         |
|         10 |        2 | DVCS               |
|         11 |        2 | SVK                |
|         12 |        2 | Bazaar             |
|         13 |        2 | …
Run Code Online (Sandbox Code Playgroud)

hibernate hibernate-mapping hibernate-annotations

6
推荐指数
1
解决办法
1万
查看次数

java.lang.NoClassDefFoundError:org/apache/commons/pool/impl/GenericObjectPool

我正在尝试使用tomcat 6和postgresql 9.1在一个tomcat项目下将org.apache.commons.dbcp.BasicDataSource配置为web.xml中的bean

我的servletdispacher.xml

`

<bean id="viewResolver"
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"
      p:prefix="/WEB-INF/jsp/"
      p:suffix=".jsp" />




<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://localhost:5432/car" />
    <property name="username" value="postgres" />
    <property name="password" value="123" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="annotatedClasses">
        <list>
            <value>DAOModel.Tblusers</value>
        </list>
    </property>
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="DAOModel.Tblusers" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

得到的错误:

javax.servlet.ServletException: Servlet.init() for servlet dispatcher threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
java.lang.Thread.run(Thread.java:724)
Run Code Online (Sandbox Code Playgroud)

java spring-mvc hibernate-annotations spring-transactions

6
推荐指数
2
解决办法
4万
查看次数

org.hibernate.AnnotationException:外键引用的列数错误.应该是2

表详情

我有如上面屏幕截图中的表格

课程编写如下

@Entity  
public class Object {  
    @Id  
    private int id;  

    private String name;  

    @OneToMany(mappedBy="object",fetch=FetchType.LAZY)  
    private List<ObjectAttribute> attrubuteList;  
}  

@Entity  
public class ObjectAttribute {  
    @Id  
    private int id;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  
    private String name;  
}  

@Entity  
public class Filter {  
    @Id  
    private int filterId;  
    @ManyToOne  
    @JoinColumn(name="ObjectId")  
    private Object object;  
    private String filterName;  
    @OneToMany(mappedBy="filter")  
    private Set<FilterAttribute> filterValues;  
}  

@Entity  
public class FilterAttribute implements Serializable {  

    @Id  
    private int filterAttrId;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="objectId")  
    private Object object;  
    @Id  
    @ManyToOne  
    @JoinColumn(name="filterId") …
Run Code Online (Sandbox Code Playgroud)

java hibernate hibernate-mapping hibernate-annotations

6
推荐指数
1
解决办法
2万
查看次数

JPA/Hibernate @OrderBy注释包含多个列和每个ASC/DESC

我想用带有多列的@OrderBy注释对OneToMany字段进行排序,并为每个列指定排序顺序,但我似乎无法在任何地方找到有关如何或不可能的信息.注释的规格说:

orderby_list::= orderby_item [,orderby_item]*
orderby_item::= property_or_field_name [ASC | DESC]
Run Code Online (Sandbox Code Playgroud)

所以我的猜测是不可能的,但无论如何我更愿意问.

在部署时抛出以下内容会引发HibernateException:

@OrderBy("field1 DESC, field2 DESC, field3 DESC, field4 DESC")
Run Code Online (Sandbox Code Playgroud)

生成:

Caused by: org.hibernate.HibernateException: Unable to parse order-by fragment
Run Code Online (Sandbox Code Playgroud)

谢谢

jpa hibernate-annotations jpa-annotations

6
推荐指数
1
解决办法
1万
查看次数