我对 hibernate 很陌生,我正在尝试将我拥有的 JDBC 项目转换为 Hibernate。
我正在使用注释,我设法注释了基本的东西,但是,我现在被困在更重的对象上,我不知道如何注释它们。这是类:
@Entity
@Table(name = "person")
public class Person {
public Person{
}
// THIS WILL BE SOON INJECTED BY SPRING
private static transient PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
private static transient EmailValidator validator = EmailValidator.getInstance();
@Id
@Column(name = "person_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(name = "private_name", nullable = false, length = 20)
private String privateName;
@Column(name = "middle_name", length = 20)
private String middleName;
@Column(name = "family_name", nullable = false, length = 20) …Run Code Online (Sandbox Code Playgroud) 在我的设计中,我的所有daos都继承自父类,这个父类包含hibernateTemplate字段和一个setSessionFactory,它在使用spring设置会话时创建hibernateTemplate
这里的问题是,即使它似乎已经设置但是当我实际执行代码并且调用daos时,hibernateTemplate对象似乎为null.但是,当我使用会话工厂注入Dao对象而不是父通用类时,它就像一个魅力
AbstractDaoSupport类的一部分
/** The hibernate template. */
private HibernateTemplate hibernateTemplate;
/**
* Sets the session factory.
*
* @param sessionFactory the new session factory
*/
public void setSessionFactory(SessionFactory sessionFactory) {
this.setHibernateTemplate(new HibernateTemplate(sessionFactory));
}
/**
* Sets the hibernate template.
*
* @param hibernateTemplate the hibernateTemplate to set
*/
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
Run Code Online (Sandbox Code Playgroud)
这是当前有问题的代码,其中hibernateTemplate在运行时为null
<!-- the DataSource for application usage -->
<bean id="applicationDataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/taxidb"/>
<property name="username" value="root"/>
<property …Run Code Online (Sandbox Code Playgroud) 我正在开发我的第一个YII网站.我对YII数据库有一些疑问.
在yii中有三种查询数据库的方法.
在Query Builder查询的情况下,我们明确选择表格
$user = Yii::app()->db->createCommand()
->select('id, username, profile')
->from('tbl_user') // explicitly choosing the table
->join('tbl_profile p', 'u.id=p.user_id')
->where('id=:id', array(':id'=>$id))
->queryRow();
Run Code Online (Sandbox Code Playgroud)
那么我应该在哪里编写查询构建器查询?如果我在相应的表模型中写它们有什么好处吗?
如果我使用DAO或查询构建器,哪个类应该扩展我的模型?
如果我遵循DAO方法或查询构建器方法,如何验证用户输入?
我有 1 个实体调用 Item,我希望能够将父项链接到子项。使用连接表来创建父/子关系。我一直无法获得任何好的文档。因此,如果有人有任何想法,我会全神贯注。
这是我所拥有的......大部分时间都有效。
public class Item implements java.io.Serializable {
@Id
private Long id;
@ManyToOne(optional = true, fetch = FetchType.LAZY)
@JoinTable(name = "ITEMTOITEM", joinColumns = { @JoinColumn(name = "ITEMID") }, inverseJoinColumns = { @JoinColumn(name = "PARENTITEMID") } )
private Item parent;
@OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
private List<Item> children;
}
Run Code Online (Sandbox Code Playgroud)
有时,当我想带回与此项目表相关联的对象时,我会收到以下错误:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.webflow.engine.ActionExecutionException: Exception thrown executing [AnnotatedAction@6669ff5 targetAction = com.assisted.movein.web.common.nav.NavAction@6edf74b7, attributes = map['method' -> 'handleEntry']] in state 'oneTimeChargesAndFeesView' of flow 'in-flow' -- …Run Code Online (Sandbox Code Playgroud) 好吧,我的项目中有些东西困扰着我。我有很多 hibernate 实体类,每个类都有自己的 DAO(继承自 GenericDAO)。它们中的大多数没有特定的功能,只是一个继承 GenericDAO 的空类。
因为我相信那些是不必要的类,所以我决定使用反射来摆脱它们。经过一些编码后,我对除 GenericDAO 之外没有特定方法的所有类的调用都遵循以下设计:
DAO.forClass(MyClass.class, MyClassPK.class).genericDAOMethod();
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力。我现在摆脱了空的 DAO。但是在通过互联网搜索后,我发现像我这样的解决方案几乎没有,所以问题是:
这种方法在任何相当大的方面是错误的还是坏的?为什么从来没有人考虑过做这样的事情?
EntityManager 和 DAO 有什么区别?是一样的吗?
我正在尝试使用 android-room 对我的 DAO 进行单元测试。我编写了一个可以正常工作的插入测试。不幸的是,删除方法似乎不起作用。
我已经尝试了几种不同的测试设置。没有一个工作。
这是 DAO:
@Dao
public interface MonthlyDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void saveAll(List<Monthly> goals);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void save(Monthly goal);
@Update
void update(Monthly goal);
@Delete
void delete(Monthly goal);
@Query("SELECT * FROM Monthly")
LiveData<List<Monthly>> findAll();
@Query("SELECT * FROM monthly")
List<Monthly> findAllList();
}
Run Code Online (Sandbox Code Playgroud)
这是每月实体:
@Entity
public class Monthly {
@PrimaryKey(autoGenerate = true)
private int monthlyId;
@TypeConverters(CalendarTypeConverter.class)
@ColumnInfo(name = "date")
private Calendar date = Calendar.getInstance();
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "description")
private String description;
@ColumnInfo(name …Run Code Online (Sandbox Code Playgroud) 我在我的项目中使用 Kotlin 实现了 RoomDatabase。我不断收到以下错误。
error: ProductDatabase_Impl is not abstract and does not override abstract method getProductDao()
in ProductDatabase
public final class ProductDatabase_Impl extends ProductDatabase {
Run Code Online (Sandbox Code Playgroud)
这是我的 Dao 接口和 ProductDatabase:
@Dao
interface ProductDao {
@Insert
suspend fun insertProduct(product: Product) : Long
@Insert
suspend fun insertAll(products: ArrayList<Product>) : List<Long>
@Update
suspend fun updateProduct(product: Product) : Int
@Query("SELECT * FROM product_table")
fun getAllProducts() : LiveData<List<Product>>
}
@Database(entities = [Product::class], version = 1)
abstract class ProductDatabase : RoomDatabase() {
abstract fun productDao(): ProductDao
abstract …Run Code Online (Sandbox Code Playgroud) 与这篇文章类似的想法: How do I generated a 4-digit pin no duplicated number
但是,有没有办法在创建新用户时自动执行此操作,而不是将其设置为随机?
我希望能够在我的系统中创建一个新用户,并让他们的 PIN 码自动随机生成 4 位数字。AUTO_INCRMENT 是唯一的自动选项吗?
我用过这个:
UPDATE patrons SET pin=FLOOR(1000 + (RAND() *8999)) WHERE pin = ?
但这不是我想要的,因为它必须手动完成。我在 Java 程序的 SQL DAO 类中创建了一个方法,在创建用户后执行此操作,因此它确实有效,但如果我要在 Maria DB 中创建一个新用户,它只会自动递增每个值一个值时间。我希望无论我使用数据库的方式如何,随机生成都会发生。
我非常感谢一些帮助!
更新
我不需要密码是唯一的,因为用户还需要他们的图书卡号才能登录系统。
当我创建一个从表名中选择所有项目的方法时,Dao 类无法解析表名!
我根本不明白为什么会发生这种事。问题是我正在开发另一个项目(两天前!!),其中我也使用了 Room 并且它工作得很好,当我创建这个新项目并定义了旧项目下面显示的类时现在同样的问题!(哈哈)
我的对象类
package com.example.adapterapp.Entites;
import androidx.room.Entity;
import androidx.room.Index;
import androidx.room.PrimaryKey;
@Entity(tableName = "items_table")
public class RoomItem {
@PrimaryKey(autoGenerate = true)
private int itemId;
private final String itemTitle;
private final String itemSubtitle;
private final String imageUri;
public RoomItem(String itemTitle, String itemSubtitle, String imageUri) {
this.itemTitle = itemTitle;
this.itemSubtitle = itemSubtitle;
this.imageUri = imageUri;
}
public int getItemId() {
return itemId;
}
public String getItemTitle() {
return itemTitle;
}
public String getItemSubtitle() {
return itemSubtitle;
}
public String …Run Code Online (Sandbox Code Playgroud) dao ×10
java ×6
android ×3
android-room ×3
hibernate ×3
spring ×2
activerecord ×1
annotations ×1
jakarta-ee ×1
jpa ×1
jpa-2.0 ×1
kotlin ×1
mariadb ×1
overriding ×1
php ×1
reflection ×1
sql ×1
testing ×1
yii ×1