引用
转载:http://blog.sina.com.cn/s/blog_7a8f609501014j34.html
使用hibernate 注解配置实体类的关联关系,在many-to-one,one-to-one关联中,一边引用自另一边的属性,如果属性值为某某的数据在数据库不存在了,hibernate默认会抛出异常。解决此问题,加上如下注解就可以了:
@NotFound(action=NotFoundAction.IGNORE),意思是找不到引用的外键数据时忽略,NotFound默认是exception
下面贴出hibernate 注解的实例代码
view plaincopy to clipboardprint?
@Entity
@Table(name = "ICT_COMPUTER_LOCATION")
public class IctComputerLocation {
private static final long serialVersionUID = 1L;
private Integer id;
private String idcNum;
private Integer ictBaseId;
private IctBase ictBase;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ID")
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(name = "IDC_NUM")
public String getIdcNum() {
return idcNum;
}
public void setIdcNum(String idcNum) {
this.idcNum = idcNum;
}
@Column(name = "ICT_BASE_ID")
public Integer getIctBaseId() {
return ictBaseId;
}
public void setIctBaseId(Integer ictBaseId) {
this.ictBaseId = ictBaseId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "ICT_BASE_ID", referencedColumnName = "ID", unique = false, nullable = false, insertable = false, updatable = false)
@NotFound(action=NotFoundAction.IGNORE)
public IctBase getIctBase() {
return ictBase;
}
public void setIctBase(IctBase ictBase) {
this.ictBase = ictBase;
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hardwin/archive/2009/07/08/4330779.aspx
对象A一对多对象B,现在要查询对象A,条件中包含对象B的属性,但对象A的属性B可能为Null,这时候怎么写条件?
比如对象A是用户:
Java code
@Entity
@Table(name = "base_staff")
public class User {
@Id
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
@Column(name = "logname")
private String logname;
@Column(name = "logpass")
private String logpass;
@ManyToOne
@JoinColumn(name = "gender")
@NotFound(action=NotFoundAction.IGNORE)
private Gender gender;
@Column(name = "birthdate")
private Date birthdate;
@ManyToOne
@JoinColumn(name = "user_status")
@NotFound(action=NotFoundAction.IGNORE)
private UserStatus status;
@Column(name = "note")
private String note;
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(name = "sys_user_role",
joinColumns = { @JoinColumn(name = "user_id") },
inverseJoinColumns = { @JoinColumn(name = "role_id") })
@NotFound(action=NotFoundAction.IGNORE)
private Set roles;
……
}
对象B是性别:
Java code
@Entity
@Table(name = "code_gender")
public class Gender {
@Id
@Column(name = "id")
private int id;
@Column(name = "name")
private String name;
}
UserDao:
Java code
Query query = session.createQuery("from User u" +
" where u.name like :key" +
" or u.gender.name like :key" +
" or u.logname like :key" +
" or u.status.name like :key" +
" or u.note like :key" +
" order by u.id");
query.setParameter("key", "%" + key + "%");
query.setFirstResult(offset);
query.setMaxResults(size);
List list = query.list();
这种情况查询不到User的gender为null的数据。想要得到那些数据应该怎么些HQL呢?
分享到:
相关推荐
如果目标数据库中已存在与导入文件相同的表,可以使用`ignore=y`选项来忽略冲突: ```sql imp 数据库用户名 / 密码 @ 连接域名 file=daochu.dmp ignore=y; ``` ### 只读权限 当为用户授予只读权限时,可以使用`...
- 当不需要完整关联数据时,可使用`@NotFound(action = NotFoundAction.IGNORE)`避免找不到关联记录时抛出异常。 7. **缓存策略** 对于多对多关联,可以配置第二级缓存,以提高数据访问速度,但需注意缓存一致性...
[ID] [int] IDENTITY(1,1) NOT NULL, [CODE] [nvarchar](50) NULL, [PARENT_CODE] [nvarchar](50) NULL, [NAME] [nvarchar](50) NULL, [SHORT_NAME] [nvarchar](50) NULL, [LNG] [decimal](18, 6) NULL, [LAT...
* JAR files found in the "server" directory under "catalina.home"), and * starts the regular execution of the container. The purpose of this * roundabout approach is to keep the Catalina internal ...
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO EXEC sys.sp_...
imp [username/password@database] file=import_file.dmp ignore=y log=import.log ``` #### 3.2 批量处理 通过脚本的方式批量处理多个表或表空间的导出导入工作: ```bash for table in "table1" "table2"; do ...
/*! * @license TweenJS ...* ...* * Distributed under the terms of the MIT license. ...* * This notice shall be included in all copies or substantial portions of the Software. ...this.createjs=this.createjs||{},...
import pandas as pd import os ... all_data = pd.concat([all_data, city_data], ignore_index = True) all_data.to_csv('merged_data.csv', index = False) print(all_data.info()) print(all_data
JUnit4引入了一些新的特性,例如`@Before`、`@After`、`@Ignore`、`@Test(expected = ...)`等注解,这些注解为测试提供了更多的灵活性和控制能力。 **示例代码:** ```java package wooyo.main; import static ...
- 新版命令格式为:`impdp aichannel/aichannel@HUST directory=DATA_PUMP_DIR dumpfile=newsmgnt.dmp ignore=y` 以上就是关于Oracle中DMP文件的导入导出的基本操作介绍。需要注意的是,新版命令中通常会使用`...
$ imp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1,table2 fromuser=dbuser touser=dbuser2 commit=y ignore=y ``` 2. **用户方式**:导出或导入指定用户的全部对象和数据。 - **导出:** ``` $ ...
IMP userid=accp/accp@orcl tables=(bank) file=D:\dmp.dmp ignore=y; ``` - **按用户导入**:可以将数据导入到指定用户的账户下。 ```sql IMP userid=accp/accp@orcl file=D:\dmp.dmp; ``` 通过上述步骤,...
pip install --ignore-installed --upgrade tensorflow ``` 二、基本使用 安装完成后,可以使用TensorFlow进行基本的机器学习操作。例如,以下代码定义了一个字符串常量hello,并使用Session对象打印结果: ``` ...
全库方式:将数据库中的所有对象导出或导入,例如,exp user/pwd file=/dir/xxx.dmp log=xxx.log full=y commit=y ignore=y。 四、高级选项 exp 和 imp 工具都有多种高级选项,例如: 分割成多个文件:exp user/...
例如,可以通过指定@Formula来使用原生SQL查询来覆盖某些特定的计算字段,或者使用@NotFound(action=NotFoundAction.IGNORE)来优化外键关联的性能问题。 9. **元数据提取**:Hibernate可以自动从注解中提取元数据,...
warnings.filterwarnings("ignore") from matplotlib.pyplot import figure conn = pyodbc.connect('Driver={SQL Server};Server=LAPTOP-78J09PRS\SQLEXPRESS;Database=Restaurant_rating;Trusted_Connection=yes;')...
导入示例:`$imp user/pwd file=/dir/xxx.dmp log=xxx.log tables=table1,table2 fromuser=dbuser touser=dbuser2 commit=y ignore=y` 2. **用户方式**:将指定用户的全部对象及数据进行导出或导入。 导出示例:...
如果想要忽略导入过程中遇到的错误,则可以添加`ignore=y`参数: ```bash imp system/manager@dbname file=backup.dmp ignore=y ``` 同样地,如果只想导入特定的表,可以通过`tables`参数来指定: ```bash imp ...