在其他人之前问过这个问题时道歉,但是其他解决方案似乎使用的是@ComponentScan,这是针对Spring的,我使用的是dropwizard,因此对我不起作用。
这是我(非常简单)的DTO课程。
package apidto.entity.organization;
import lombok.Data;
import javax.persistence.*;
@Entity
@Table(name = "organization")
@NamedQueries({@NamedQuery( name =
"apidto.entity.organization.Organization.findAll",
query = "SELECT o from Organization o")})
public @Data class Organization {
@Id
private long id;
@Column(name = "name")
private String name;
}
我有以下DAO课程......
import apidto.entity.organization.Organization;
import io.dropwizard.hibernate.AbstractDAO;
import org.hibernate.SessionFactory;
import java.util.List;
public class OrganizationDAO extends AbstractDAO<Organization> {
public OrganizationDAO(SessionFactory sessionFactory)
{
super(sessionFactory);
}
public List<Organization> findAll()
{
return list(namedQuery("apidto.entity.organization.Organization.findAll"));
}
}
答案 0 :(得分:0)
您应该在ScanningHibernateBundle
课程中使用Application
。 ScanningHibernateBundle
将扫描apidto.entity
包和所有嵌套包,并将所有带@Entity
注释的类添加到Hibernate SessionFactory
。
class SomeApp extends Application<SomeConfiguration> {
@Override
public void initialize(Bootstrap<SomeConfiguration> bootstrap) {
HibernateBundle<SomeConfiguration> hibernate = new ScanningHibernateBundle<SomeConfiguration>(
"apidto.entity") {
@Override
public PooledDataSourceFactory getDataSourceFactory(SomeConfiguration configuration) {
return configuration.getDataSourceFactory();
}
};
bootstrap.addBundle(hibernate);
}
}
答案 1 :(得分:0)
我在使用Dropwizard和Hibernate时遇到了同样的问题。我有一个这样的User实体类:
@Entity
@Table(name = "users")
@NamedQueries({
@NamedQuery(name = "com.majidkhorsandi.dropbookmarks.core.User.findAll",
query = "SELECT u FROM User u"),
@NamedQuery(name = "com.majidkhorsandi.dropbookmarks.core.User.findByUsernamePassword",
query = "SELECT u FROM User u WHERE u.username = :username AND u.password = :password")
})
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String username;
private String password;
/*private List<Bookmark> bookmarks = new ArrayList<>();*/
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
...
然后我的DAO课为:
public class UserDAO extends AbstractDAO<User> {
UserDAO(SessionFactory sessionFactory) {
super(sessionFactory);
}
List findAll() {
return list(namedQuery("com.majidkhorsandi.dropbookmarks.core.User.findAll"));
}
public Optional<User> findByUsernamePassword(String username, String password) {
return Optional.ofNullable(uniqueResult(
namedQuery("com.majidkhorsandi.dropbookmarks.core.User.findByUsernamePassword")
.setParameter("username", username)
.setParameter("password", password)
));
}
}
然后是我的hibernate.cfg.xml的外观:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1</property>
<property name="connection.username">sa</property><!--./target/example-->
<property name="connection.password">sa</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">10</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Enable Hibernate's current sessions tracking by thread of execution -->
<property name="current_session_context_class">managed</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<mapping class="com.majidkhorsandi.dropbookmarks.core.User"/>
</session-factory>
</hibernate-configuration>
我认为hibernate.cfg.xml中的映射标记应足以完成User
的映射:
<mapping class="com.majidkhorsandi.dropbookmarks.core.User"/>
但显然不是。因此,我要做的是使用休眠配置(在我的情况下为SessionFactory
)中向构建HibernateUtil
的类中添加以下额外的行:
configuration.addAnnotatedClass(User.class);
所以最终,这是修复后我的HibernateUtil
的样子:
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
Configuration configuration = new Configuration().configure();
configuration.addAnnotatedClass(User.class);
StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
static SessionFactory getSessionFactory() {
return sessionFactory;
}
}