我已经检查了有关此事的其他主题并尝试了一些标准解决方案,但似乎没有任何效果。我的数据库是本地的,我正在尝试连接,它已经创建并保存在名为 localDB.mdf 的文件夹 Helpers 中我的代码是:
public static List<ChatMessages> GetFromDatabase(string query)
{
List<ChatMessages> ImportedFiles = new List<ChatMessages>();
string sql = @"Data Source=|DataDirectory|\Helpers\localDB;AttachDbFilename=\Helpers\localDB.mdf;Integrated Security=True";
SqlConnection connection = new SqlConnection(sql);
try
{
connection.Open();
var check = connection.State;
SqlCommand cmd = new SqlCommand(query, connection);
var rdr = cmd.ExecuteReader();
while (rdr.Read())
{
//stuff
}
connection.Close();
}
catch (Exception e) { MessageBox.Show("error: " + e); }
var x = ImportedFiles;
return ImportedFiles;
}
Run Code Online (Sandbox Code Playgroud)
感谢您的时间,最好的问候
我正在尝试按照https://vladmihalcea.com/the-best-way-to-map-a-onetoone-relationship-with-jpa-and-hibernate/进行 @OneToOne 映射,映射本身有效,但它触发N+1查询问题。
正在对父实体服务进行查询并触发 N+1 查询。 如何改进此代码以仅进行 1 个查询?在这种情况下,我们不需要访问 ParentDetails。
编辑:我尝试过使用 JPQL,但LEFT JOIN FETCH ParentDetails也不起作用。
EDIT2:只是尝试添加更多信息。我在 getParentDetails 上设置了一个断点,只是为了确保我没有在任何地方调用 getter,并且我没有调用并仔细检查,这似乎是存储库调用上的连接问题。
我们来看代码:
家长
@Entity
@DynamicUpdate
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@OneToOne(fetch = FetchType.LAZY,
cascade = CascadeType.ALL,
mappedBy = "parent")
private ParentDetails parentDetails;
// Getters, setters, etc omitted for brevity
}
Run Code Online (Sandbox Code Playgroud)
家长详情
@Entity
public class ParentDetails {
@Id
private Long id;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
private Parent parent;
// Getters, setters, etc omitted …Run Code Online (Sandbox Code Playgroud)