jpa oracle boolean,配置休眠(使用JPA)来存储类型为布尔值而不是0/1的Y/N.

Jim Tough..

11

我使用了@marcg发布的答案中的概念,它与JPA 2.1一起使用效果很好.他的代码不太正确,所以我发布了我的工作实现.这会将Boolean实体字段转换为数据库中的Y/N字符列.

从我的实体类:

@Convert(converter=BooleanToYNStringConverter.class)

@Column(name="LOADED", length=1)

private Boolean isLoadedSuccessfully;

我的转换器类:

/**

* Converts a Boolean entity attribute to a single-character

* Y/N string that will be stored in the database, and vice-versa

*

* @author jtough

*/

public class BooleanToYNStringConverter

implements AttributeConverter {

/**

* This implementation will return "Y" if the parameter is Boolean.TRUE,

* otherwise it will return "N" when the parameter is Boolean.FALSE.

* A null input value will yield a null return value.

* @param b Boolean

*/

@Override

public String convertToDatabaseColumn(Boolean b) {

if (b == null) {

return null;

}

if (b.booleanValue()) {

return "Y";

}

return "N";

}

/**

* This implementation will return Boolean.TRUE if the string

* is "Y" or "y", otherwise it will ignore the value and return

* Boolean.FALSE (it does not actually look for "N") for any

* other non-null string. A null input value will yield a null

* return value.

* @param s String

*/

@Override

public Boolean convertToEntityAttribute(String s) {

if (s == null) {

return null;

}

if (s.equals("Y") || s.equals("y")) {

return Boolean.TRUE;

}

return Boolean.FALSE;

}

}

如果您喜欢表情符号,并且只是厌倦了数据库中的Y/N或T/F,这个变体也很有趣.在这种情况下,您的数据库列必须是两个字符而不是一个.可能不是什么大不了的事.

/**

* Converts a Boolean entity attribute to a happy face or sad face

* that will be stored in the database, and vice-versa

*

* @author jtough

*/

public class BooleanToHappySadConverter

implements AttributeConverter {

public static final String HAPPY = ":)";

public static final String SAD = ":(";

/**

* This implementation will return ":)" if the parameter is Boolean.TRUE,

* otherwise it will return ":(" when the parameter is Boolean.FALSE.

* A null input value will yield a null return value.

* @param b Boolean

* @return String or null

*/

@Override

public String convertToDatabaseColumn(Boolean b) {

if (b == null) {

return null;

}

if (b) {

return HAPPY;

}

return SAD;

}

/**

* This implementation will return Boolean.TRUE if the string

* is ":)", otherwise it will ignore the value and return

* Boolean.FALSE (it does not actually look for ":(") for any

* other non-null string. A null input value will yield a null

* return value.

* @param s String

* @return Boolean or null

*/

@Override

public Boolean convertToEntityAttribute(String s) {

if (s == null) {

return null;

}

if (HAPPY.equals(s)) {

return Boolean.TRUE;

}

return Boolean.FALSE;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中使用Hibernate JPA,您可以使用Spring Boot提供的HibernateJpaConfiguration类进行配置。以下是一个简单的示例,演示如何使用Hibernate JPA和Spring Boot配置JDBC连接: 1. 添加Hibernate JPA和MySQL JDBC的依赖项: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> ``` 2. 在application.properties文件中添加以下属性: ``` spring.datasource.url=jdbc:mysql://localhost:3306/db_name spring.datasource.username=db_username spring.datasource.password=db_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=update ``` 3. 创建一个HibernateJpaConfiguration类,继承自Spring Boot提供的HibernateJpaConfiguration类: ``` @Configuration @EnableTransactionManagement public class MyHibernateJpaConfiguration extends HibernateJpaConfiguration { @Autowired private DataSource dataSource; @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean(); em.setDataSource(dataSource); em.setPackagesToScan("com.example.demo.entity"); JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); em.setJpaVendorAdapter(vendorAdapter); Properties properties = new Properties(); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect"); em.setJpaProperties(properties); return em; } @Bean public PlatformTransactionManager transactionManager() { JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } } ``` 4. 确保您的实体类在指定的包中,并且使用@Entity和@Id注释进行注释: ``` @Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name = "name") private String name; @Column(name = "email") private String email; // getters and setters } ``` 5. 在您的服务类中使用@Autowired注释自动装配EntityManager: ``` @Service public class UserService { @Autowired private EntityManager entityManager; @Transactional public User save(User user) { entityManager.persist(user); return user; } @Transactional(readOnly = true) public User findById(Long id) { return entityManager.find(User.class, id); } // other methods } ``` 这个例子展示了如何使用Hibernate JPA和Spring Boot配置JDBC连接。您可以根据自己的需要进行调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值