我想问一下如何使用诸如@AuditOverride,@Audited或 else 之类的休眠注释来审计实体超类的一部分。现在,我使用的是 hibernate 5.2.12 版本。
我只能在子类中使用的注释,因为超类在其他模块中,它不应该知道关于子模块的任何信息。
超类包含一个List<Items>,我不希望它被审计。因为当我使用one-to-many关系时,我不希望休眠会创建审计关系表,例如entity1_aud_entity2_aud。我只需要entity1_aud和entity2_aud表。
拒绝审计关系表我找到了两种方法,但都不完全正确:
我将列表变量和 setter/getter 复制到实体(subclass)。上面的列表变量我写了@NotAudited注释。为了使该注释起作用,我access="field"在hbm文件中设置了属性。因此休眠不使用 setter 和 getter 访问变量,因此在数据拉取期间未设置超类的值。
我还创建了我写的列表实体@AuditOverrides(value={@AuditOverride(forClass=Entity2.class), @AuditOverride(forClass=Item.class)})。这些注释为列表实体创建审计表。所以这种审计方式的完整代码是:
Entity1.class (main sublcass) [hibernate module]
@AuditOverrides(value = {
@AuditOverride(forClass = Entity1.class),
@AuditOverride(forClass = Superclass.class, name = "list", isAudited = false)
})
public class Entity1 extends Superclass {
@NotAudited
private List<Item> list = new ArrayList<>();
@Override …Run Code Online (Sandbox Code Playgroud) 我有一个类来映射一个使用休眠的表。有一些变量我想忽略以用于映射以用作常量。我想从属性加载常量值,所以我这样编码:
@Transient
@Value("${something.value}")
private int MY_VALUE;
Run Code Online (Sandbox Code Playgroud)
但是,的值MY_VALUE始终设置为 0。我不能将 @Transient 注释与 @Value 注释一起使用吗?还是我错过了其他东西?
java spring hibernate spring-annotations hibernate-annotations
我hbm2ddl在基于hibernate的应用程序中使用生成数据库模式.hibernate.hbm2ddl.auto财产的价值是create-drop.
我正在@Entity为我的POJO课程使用注释.
@Entity
public class testTable1 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
Long id;
}
@Entity
public class testTable2 {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
Long id;
}
Run Code Online (Sandbox Code Playgroud)
但是在执行代码时,我不断获得增量Id值.例如,对于2个表,Id(即Prim Key)应该以1开始.但是在插入记录之后Table 1,序列从下一个值开始Table 2.它应该从表2的1开始.我试过GenerationType.SEQUENCE&GenerationType.AUTO.什么都行不通:-(
我一直在通过不同的论坛搜索问题,并尝试了不同的解决方案,但我仍然无法找到任何正确的答案我的问题.
我正在使用hibernate4注释来映射我的实体.一切正常,但在mysql中使用hibernate创建表时,只检测不到自动增量键.
我有以下代码:
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true, nullable = false)
private int responseId;
Run Code Online (Sandbox Code Playgroud)
我也试过了
@Id
@GenericGenerator(name="generator", strategy="increment")
@GeneratedValue(generator="generator")
private int responseId;
Run Code Online (Sandbox Code Playgroud)
使用hibernate它可以正常工作,id会自动分配给row,但在mysql表中它没有AutoIncrement Constraint.我必须手动将字段标记为AI.当我手动插入记录以进行测试或使用表的jdbc语句时,这会成为问题.Plz让我知道我在配置中缺少的是阻止hibernate在各个列上施加AutoIncrement Contraint.
问题:
为什么更改joinColumn的顺序,hibernate返回正确或不正确的查询?
第一次加入:
@OneToOne
@JoinTable(name="TTR_POA_UPA",
joinColumns = {
@JoinColumn(name="ANO", insertable=false, updatable=false),
@JoinColumn(name="ID_ESCALA", insertable=false, updatable=false),
@JoinColumn(name="MES", insertable=false, updatable=false)
},
inverseJoinColumns = {
@JoinColumn(name="ANO", insertable=false, updatable=false),
@JoinColumn(name="ID_ESCALA", insertable=false, updatable=false),
@JoinColumn(name="MES", insertable=false, updatable=false)
}
)
private PoaUpa poaUpa;
Run Code Online (Sandbox Code Playgroud)
返回:
Hibernate: select * from ( select autorizaci0_.ano as ano0_, autorizaci0_.ID_ESCALA as ID2_0_, autorizaci0_.mes as mes0_, autorizaci0_.AUTORIZACION_HORAS_DIA_FIJIS as AUTORIZA4_0_, autorizaci0_.AUTORIZACION_HORAS_EVENT as AUTORIZA5_0_, autorizaci0_.AUTORIZACION_HORAS_FACTP as AUTORIZA6_0_, autorizaci0_.AUTORIZACION_HORAS_FIJIS as AUTORIZA7_0_, autorizaci0_.AUTORIZACION_HORAS_FIJOS as AUTORIZA8_0_, autorizaci0_.AUTORIZACION_HORAS_SEM_FACTP as AUTORIZA9_0_, autorizaci0_.AUTORIZACION_NAP as AUTORIZ10_0_, autorizaci0_.AUTORIZACION_NUM_PERS_EVENT as AUTORIZ11_0_, autorizaci0_.AUTORIZACION_OBSERVACIONES as AUTORIZ12_0_, autorizaci0_.PETICION_HORAS_DIA_FIJIS as …Run Code Online (Sandbox Code Playgroud) java spring hibernate spring-annotations hibernate-annotations
我有以下 JPA 实体
@SQLDelete(sql="UPDATE service SET date_deletion = CURRENT_DATE() WHERE id = ?")
@Where(clause="date_deletion IS NULL ")
public class Service {
...
}
Run Code Online (Sandbox Code Playgroud)
选择工作确定所有带有 date_deletion 通知的元素都没有显示,但是当我尝试删除时....
16:38:26,836 DEBUG SQL:111 - UPDATE service SET date_deletion = CURRENT_DATE() WHERE id = ?
16:38:26,836 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
16:38:26,836 DEBUG JDBCExceptionReporter:225 - could not delete: [com.foo.domain.Service#1] [UPDATE service SET date_deletion = CURRENT_DATE() WHERE id = ?]
java.sql.SQLException: Parameter index out of range (2 > number …Run Code Online (Sandbox Code Playgroud) 我试图在关系表股票类别中插入一行.
我跟随这个例子:http://www.mkyong.com/hibernate/hibernate-many-to-many-example-join-table-extra-column-annotation/
现在我已经拥有表格库存和类别中的数据.
后来我想把股票和类别相互关联起来.
如何在不编写自定义sql查询的情况下执行此操作?
我可以像这样添加StockCategory吗?
Stock stock = new Stock();
stock.setStockId(1);
Category category = new Category();
category.setCategoryId(1);
StockCategory stockCategory = new StockCategory();
stockCategory.setStock(stock); //here you need to get the stock object by id
stockCategory.setCategory(category1); //here you need to get the category1 object by id
stockCategory.setCreatedDate(new Date()); //extra column
stockCategory.setCreatedBy("system"); //extra column
session.save(stockCategory );
Run Code Online (Sandbox Code Playgroud)
提前致谢.
kotlin 中的以下注释用法相当于什么:
@TypeDefs({
@TypeDef(name = "string-array", typeClass = StringArrayType.class),
@TypeDef(name = "int-array", typeClass = IntArrayType.class),
@TypeDef(name = "json", typeClass = JsonStringType.class),
@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class)
})
Run Code Online (Sandbox Code Playgroud)
这是@TypeDefs 的定义
@Target({TYPE, PACKAGE})
@Retention(RUNTIME)
public @interface TypeDefs {
/**
* The grouping of type definitions.
*/
TypeDef[] value();
}
public class Foo {...}
Run Code Online (Sandbox Code Playgroud)
这是@TypeDef 之一
@Target({TYPE, PACKAGE})
@Retention(RUNTIME)
@Repeatable(TypeDefs.class)
public @interface TypeDef {
/**
* The type name. This is the name that would be used in other locations.
*/
String …Run Code Online (Sandbox Code Playgroud) 我在类中有一些注释,例如
public class ProductModel {
@Pattern(regexp="^(1|[1-9][0-9]*)$", message ="Quantity it should be number and greater than zero")
private String quantity;
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器中
@Controller
public class Product Controller
private ProductService productService;
@PostMapping("/admin/product")
public String createProduct(@Valid @ModelAttribute("product") ProductModel productModel, BindingResult result)
{
// add println for see the errors
System.out.println("binding result: " + result);
if (!result.hasErrors()) {
productService.createProduct(productModel);
return "redirect:/admin/products";
} else {
return "product";
}
}
Run Code Online (Sandbox Code Playgroud)
然后我尝试从 ProductController 测试 createProduct。
@RunWith(MockitoJUnitRunner.class)
public class ProductControllerTest {
@Autowired
private MockMvc mockMvc;
@Mock
ProductService productService;
@InjectMocks …Run Code Online (Sandbox Code Playgroud) spring unit-testing spring-mvc mockito hibernate-annotations
我希望UserAcounts能够拥有许多UserGroup.并且所有组都可以有许多Users.And有一个连接表.我希望在删除useraccount时删除连接表中useraccount和usergroup之间的关联.
实际上我想使用"删除级联".在ManyToMany关系中,我不幸运行它.我已经尝试了很多东西,但我找不到解决方案.
注意:我只想在删除级联时删除Relation
是否有办法做到这一点?
这是我的hibernate类
@SuppressWarnings("serial")
@Entity
@Table(name = "USER_ACCOUNT")
public class UserAccount implements Serializable {
@Id
@Column(name = "ID")
@GeneratedValue
private Long id;
@Column(name = "NAME")
private String name;
@Column(name = "SURNAME")
private String surname;
@Column(name = "EMAIL")
private String email;
@Column(name = "USER_NAME")
private String username;
@Column(name = "PASSWORD")
private String password;
@Column(name = "ENABLED")
@Type(type = "yes_no")
private boolean enabled;
@Column(name = "ACCOUNT_NON_EXPIRED")
@Type(type = "yes_no")
private boolean accountNonExpired;
@Column(name = "CREDENTIALS_NON_EXPIRED")
@Type(type = "yes_no")
private boolean …Run Code Online (Sandbox Code Playgroud) hibernate ×9
java ×4
spring ×3
annotations ×1
hbm2ddl ×1
jpa ×1
kotlin ×1
mockito ×1
mysql ×1
spring-mvc ×1
spring-roo ×1
unit-testing ×1